load libraries

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ purrr   1.0.0 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.5.0 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(haven)
library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## 
## The following object is masked from 'package:purrr':
## 
##     lift
library(nnet)
library(broom)
library(pROC)
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## 
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
library(rpart.plot)
## Loading required package: rpart

import the processed data

# load the processed  state data
df_part1 = read_csv("./data/df_a_m.csv")
## Rows: 775895 Columns: 24
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): sitename
## dbl (23): year, age, sex, grade, race4, bmi, qnothhpl, q85, q17, q21, q23, q...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
df_part2 = read_csv("./data/df_n_z.csv")
## Rows: 754495 Columns: 24
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): sitename
## dbl (23): year, age, sex, grade, race4, bmi, qnothhpl, q85, q17, q21, q23, q...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# merge two state dataset
df_f_project = bind_rows(df_part1, df_part2)

# load the district dataset
df_district = read_csv("./data/df_district.csv")
## Rows: 504249 Columns: 24
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): sitename
## dbl (23): year, age, sex, grade, race4, bmi, qnothhpl, q85, q17, q21, q23, q...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

data cleaning

# the state data
df_fit_state <- 
  df_f_project %>% 
  filter(year == 2019) %>% 
  mutate(
  across(c("sex", "age", "grade", "race4", "qnothhpl"), as.factor),
  year = as.character(year)
  ) %>% 
  mutate_at(vars(starts_with("q")), as.factor) %>% 
  filter(q85 %in% c(1, 2, 3)) %>% 
  na.omit()

# the school distrcit data
df_fit_district <- 
  df_district %>% 
  filter(year == 2019) %>%
    mutate(
  across(c("sex", "age", "grade", "race4", "qnothhpl"), as.factor),
  year = as.character(year)
  ) %>% 
  mutate_at(vars(starts_with("q")), as.factor) %>%
  filter(q85 %in% c(1, 2, 3)) %>% 
  na.omit()

df_fit_state %>% 
  group_by(sitename) %>% 
  summarize(
    n_obs = n()
  )
## # A tibble: 12 × 2
##    sitename            n_obs
##    <chr>               <int>
##  1 Alabama (AL)          308
##  2 Arkansas (AR)         304
##  3 Illinois (IL)         398
##  4 Iowa (IA)             235
##  5 Kentucky (KY)         313
##  6 Michigan (MI)         582
##  7 Mississippi (MS)      265
##  8 Nebraska (NE)         201
##  9 Oklahoma (OK)         348
## 10 Pennsylvania (PA)     440
## 11 South Carolina (SC)   126
## 12 West Virginia (WV)    284
df_fit_district %>% 
  group_by(sitename) %>% 
  summarize(
    n_obs = n()
  )
## # A tibble: 15 × 2
##    sitename                     n_obs
##    <chr>                        <int>
##  1 Broward County, FL (FT)        168
##  2 Chicago, IL (CH)               157
##  3 Eaton Consortium, MI (EA)      129
##  4 Fort Worth, TX (FW)            237
##  5 Genesee Consortium, MI (GE)    166
##  6 Hillsborough County, FL (HL)   248
##  7 Los Angeles, CA (LO)           109
##  8 Newark, NJ (NW)                142
##  9 Orange County, FL (OL)         123
## 10 Palm Beach County, FL (PB)     305
## 11 Pasco County, FL (PS)          200
## 12 Philadelphia, PA (PH)          172
## 13 Portland, OR (PO)              213
## 14 San Francisco, CA (SF)         203
## 15 Shelby County, TN (ST)         160
summary(df_fit_district)
##    sitename             year           age     sex      grade    race4   
##  Length:2732        Length:2732        1:  0   1:1485   1: 276   1: 818  
##  Class :character   Class :character   2:  0   2:1247   2: 610   2: 581  
##  Mode  :character   Mode  :character   3:111            3: 825   3:1036  
##                                        4:421            4:1021   4: 297  
##                                        5:730                             
##                                        6:946                             
##                                        7:524                             
##       bmi        qnothhpl q85           q17       q21      q23      q26     
##  Min.   :13.58   1: 735   1: 715   1      :1912   1: 240   1: 414   1: 651  
##  1st Qu.:20.38   2:1997   2:1849   2      : 345   2:2299   2:2318   2:2081  
##  Median :22.49            3: 168   3      : 296   3:  91                    
##  Mean   :23.64                     4      :  89   4:  55                    
##  3rd Qu.:25.72                     8      :  39   5:  17                    
##  Max.   :58.89                     5      :  28   6:  30                    
##                                    (Other):  23                             
##  q30      q41      q47      q49      q50      q52      q53      q63     
##  1: 779   1:1535   1:1577   1:2197   1:2579   1:2703   1:2690   1:   0  
##  2:1953   2: 593   2: 374   2: 230   2:  81   2:  12   2:  20   2:1512  
##           3: 340   3: 293   3: 136   3:  48   3:   3   3:   5   3:1220  
##           4: 154   4: 162   4:  77   4:   8   4:   6   4:   1           
##           5:  83   5: 144   5:  30   5:   6   5:   1   5:   7           
##           6:  15   6: 182   6:  62   6:  10   6:   7   6:   9           
##           7:  12                                                        
##  q65      q87      q89     
##  1:   0   1: 686   1: 874  
##  2:1237   2:1940   2:1066  
##  3:1226   3: 106   3: 520  
##  4: 269            4: 104  
##                    5:  50  
##                    6:  14  
##                    7: 104
summary(df_fit_state)
##    sitename             year           age      sex      grade    race4   
##  Length:3804        Length:3804        1:   0   1:2028   1: 501   1:2396  
##  Class :character   Class :character   2:   5   2:1776   2: 848   2: 546  
##  Mode  :character   Mode  :character   3: 130            3:1222   3: 550  
##                                        4: 616            4:1233   4: 312  
##                                        5:1012                             
##                                        6:1254                             
##                                        7: 787                             
##       bmi        qnothhpl q85           q17       q21      q23      q26     
##  Min.   :13.25   1:1364   1: 770   1      :2686   1: 303   1: 843   1: 948  
##  1st Qu.:20.62   2:2440   2:2822   2      : 503   2:3238   2:2961   2:2856  
##  Median :22.89            3: 212   3      : 391   3: 112                    
##  Mean   :24.20                     4      :  96   4:  90                    
##  3rd Qu.:26.51                     8      :  60   5:  29                    
##  Max.   :54.40                     5      :  38   6:  32                    
##                                    (Other):  30                             
##  q30      q41      q47      q49      q50      q52      q53      q63     
##  1:1636   1:2031   1:2473   1:3038   1:3608   1:3760   1:3737   1:   0  
##  2:2168   2: 873   2: 427   2: 323   2: 101   2:  22   2:  27   2:2001  
##           3: 445   3: 313   3: 196   3:  44   3:   3   3:  10   3:1803  
##           4: 248   4: 193   4:  97   4:  24   4:   2   4:   7           
##           5: 150   5: 145   5:  69   5:   6   5:   2   5:   3           
##           6:  30   6: 253   6:  81   6:  21   6:  15   6:  20           
##           7:  27                                                        
##  q65      q87      q89     
##  1:   0   1: 946   1:1430  
##  2:1744   2:2724   2:1396  
##  3:1765   3: 134   3: 668  
##  4: 295            4: 147  
##                    5:  50  
##                    6:  12  
##                    7: 101

check variables with 0 variance levels and re-create new variables for un-balanced variables

# the modification for school district data
levels(df_fit_district$age)
## [1] "1" "2" "3" "4" "5" "6" "7"
levels(df_fit_district$q63)
## [1] "1" "2" "3"
levels(df_fit_district$q65)
## [1] "1" "2" "3" "4"
# drop the level with 0
df_fit_district = df_fit_district %>% 
  # Remove levels from the factor column
  filter(!(age %in% c("1", "2"))) %>% 
  filter(q63 != 1) %>% 
  filter(q65 != 1) %>% 
  mutate(
  # Remove the unused levels from the factor column
     age = factor(age),
     q63 = factor(q63),
     q65 = factor(q65),
  # cover q50, q52, q53 to two level variables
  # 1 = 1, and 2 = the rest of observations
     q50 = ifelse(q50 == 1, 1, 2),
     q50 = factor(q50),
     q52 = ifelse(q52 == 1, 1, 2),
     q52 = factor(q52),
     q53 = ifelse(q53 == 1, 1, 2),
     q53 = factor(q53)
  ) 

summary(df_fit_district)
##    sitename             year           age     sex      grade    race4   
##  Length:2732        Length:2732        3:111   1:1485   1: 276   1: 818  
##  Class :character   Class :character   4:421   2:1247   2: 610   2: 581  
##  Mode  :character   Mode  :character   5:730            3: 825   3:1036  
##                                        6:946            4:1021   4: 297  
##                                        7:524                             
##                                                                          
##                                                                          
##       bmi        qnothhpl q85           q17       q21      q23      q26     
##  Min.   :13.58   1: 735   1: 715   1      :1912   1: 240   1: 414   1: 651  
##  1st Qu.:20.38   2:1997   2:1849   2      : 345   2:2299   2:2318   2:2081  
##  Median :22.49            3: 168   3      : 296   3:  91                    
##  Mean   :23.64                     4      :  89   4:  55                    
##  3rd Qu.:25.72                     8      :  39   5:  17                    
##  Max.   :58.89                     5      :  28   6:  30                    
##                                    (Other):  23                             
##  q30      q41      q47      q49      q50      q52      q53      q63     
##  1: 779   1:1535   1:1577   1:2197   1:2579   1:2703   1:2690   2:1512  
##  2:1953   2: 593   2: 374   2: 230   2: 153   2:  29   2:  42   3:1220  
##           3: 340   3: 293   3: 136                                      
##           4: 154   4: 162   4:  77                                      
##           5:  83   5: 144   5:  30                                      
##           6:  15   6: 182   6:  62                                      
##           7:  12                                                        
##  q65      q87      q89     
##  2:1237   1: 686   1: 874  
##  3:1226   2:1940   2:1066  
##  4: 269   3: 106   3: 520  
##                    4: 104  
##                    5:  50  
##                    6:  14  
##                    7: 104
# the modification for state data
levels(df_fit_state$age)
## [1] "1" "2" "3" "4" "5" "6" "7"
levels(df_fit_state$q63)
## [1] "1" "2" "3"
levels(df_fit_state$q65)
## [1] "1" "2" "3" "4"
# drop the level with 0
df_fit_state = df_fit_state %>% 
  # Remove levels from the factor column
  filter(age != 1) %>% 
  filter(q63 != 1) %>% 
  filter(q65 != 1) %>% 
  mutate(
  # Remove the unused levels from the factor column
     age = factor(age),
     q63 = factor(q63),
     q65 = factor(q65),
  # cover q50, q52, q53 to two level variables
  # 1 = 1, and 2 = the rest of observations
     q50 = ifelse(q50 == 1, 1, 2),
     q50 = factor(q50),
     q52 = ifelse(q52 == 1, 1, 2),
     q52 = factor(q52),
     q53 = ifelse(q53 == 1, 1, 2),
     q53 = factor(q53)
  ) 

summary(df_fit_state)
##    sitename             year           age      sex      grade    race4   
##  Length:3804        Length:3804        2:   5   1:2028   1: 501   1:2396  
##  Class :character   Class :character   3: 130   2:1776   2: 848   2: 546  
##  Mode  :character   Mode  :character   4: 616            3:1222   3: 550  
##                                        5:1012            4:1233   4: 312  
##                                        6:1254                             
##                                        7: 787                             
##                                                                           
##       bmi        qnothhpl q85           q17       q21      q23      q26     
##  Min.   :13.25   1:1364   1: 770   1      :2686   1: 303   1: 843   1: 948  
##  1st Qu.:20.62   2:2440   2:2822   2      : 503   2:3238   2:2961   2:2856  
##  Median :22.89            3: 212   3      : 391   3: 112                    
##  Mean   :24.20                     4      :  96   4:  90                    
##  3rd Qu.:26.51                     8      :  60   5:  29                    
##  Max.   :54.40                     5      :  38   6:  32                    
##                                    (Other):  30                             
##  q30      q41      q47      q49      q50      q52      q53      q63     
##  1:1636   1:2031   1:2473   1:3038   1:3608   1:3760   1:3737   2:2001  
##  2:2168   2: 873   2: 427   2: 323   2: 196   2:  44   2:  67   3:1803  
##           3: 445   3: 313   3: 196                                      
##           4: 248   4: 193   4:  97                                      
##           5: 150   5: 145   5:  69                                      
##           6:  30   6: 253   6:  81                                      
##           7:  27                                                        
##  q65      q87      q89     
##  2:1744   1: 946   1:1430  
##  3:1765   2:2724   2:1396  
##  4: 295   3: 134   3: 668  
##                    4: 147  
##                    5:  50  
##                    6:  12  
##                    7: 101
df_ft = df_fit_district %>% 
  filter(sitename == "Broward County, FL (FT)") %>% 
  select(-sitename, -year)

df_ch = df_fit_district %>% 
  filter(sitename == "Chicago, IL (CH)") %>% 
  select(-sitename, -year)

df_ea = df_fit_district %>% 
  filter(sitename == "Eaton Consortium, MI (EA)") %>% 
  select(-sitename, -year)
summary(df_ft)
##  age    sex     grade  race4       bmi        qnothhpl q85          q17     
##  3: 3   1:102   1: 5   1:33   Min.   :13.58   1: 38    1: 48   1      :128  
##  4: 6   2: 66   2:21   2:52   1st Qu.:19.74   2:130    2:114   2      : 23  
##  5:43           3:67   3:73   Median :21.71            3:  6   3      : 12  
##  6:77           4:75   4:10   Mean   :22.74                    4      :  4  
##  7:39                         3rd Qu.:24.35                    5      :  1  
##                               Max.   :40.18                    6      :  0  
##                                                                (Other):  0  
##  q21     q23     q26     q30     q41    q47     q49     q50     q52     q53    
##  1: 22   1: 20   1: 39   1: 41   1:88   1:103   1:137   1:161   1:166   1:168  
##  2:134   2:148   2:129   2:127   2:40   2: 19   2: 11   2:  7   2:  2   2:  0  
##  3:  6                           3:24   3: 20   3: 10                          
##  4:  6                           4: 9   4: 10   4:  3                          
##  5:  0                           5: 4   5: 11   5:  3                          
##  6:  0                           6: 3   6:  5   6:  4                          
##                                  7: 0                                          
##  q63    q65    q87     q89   
##  2:94   2:67   1: 39   1:44  
##  3:74   3:86   2:124   2:79  
##         4:15   3:  5   3:30  
##                        4: 2  
##                        5: 0  
##                        6: 1  
##                        7:12
summary(df_ft$q17)
##   1   2   3   4   5   6   7   8 
## 128  23  12   4   1   0   0   0
summary(df_ch)
##  age    sex    grade  race4       bmi        qnothhpl q85         q17     
##  3: 3   1:79   1:15   1:16   Min.   :14.46   1: 31    1:48   1      :113  
##  4:17   2:78   2:26   2:36   1st Qu.:20.81   2:126    2:98   3      : 19  
##  5:38          3:65   3:98   Median :22.98            3:11   2      : 17  
##  6:56          4:51   4: 7   Mean   :24.42                   4      :  4  
##  7:43                        3rd Qu.:26.58                   5      :  1  
##                              Max.   :39.65                   6      :  1  
##                                                              (Other):  2  
##  q21     q23     q26     q30     q41    q47    q49     q50     q52     q53    
##  1: 11   1: 15   1: 36   1: 52   1:89   1:88   1:138   1:148   1:156   1:155  
##  2:135   2:142   2:121   2:105   2:31   2:17   2:  8   2:  9   2:  1   2:  2  
##  3:  3                           3:21   3:22   3:  4                          
##  4:  5                           4:11   4:16   4:  4                          
##  5:  2                           5: 5   5: 4   5:  0                          
##  6:  1                           6: 0   6:10   6:  3                          
##                                  7: 0                                         
##  q63    q65    q87     q89   
##  2:95   2:80   1: 37   1:35  
##  3:62   3:64   2:114   2:68  
##         4:13   3:  6   3:38  
##                        4: 7  
##                        5: 2  
##                        6: 0  
##                        7: 7
summary(df_ea)
##  age    sex    grade  race4       bmi        qnothhpl q85         q17    
##  3: 3   1:61   1:15   1:86   Min.   :16.90   1:42     1:21   1      :83  
##  4:30   2:68   2:51   2: 9   1st Qu.:20.73   2:87     2:99   3      :20  
##  5:52          3:39   3:25   Median :22.91            3: 9   2      :16  
##  6:33          4:24   4: 9   Mean   :23.86                   4      : 6  
##  7:11                        3rd Qu.:25.88                   5      : 3  
##                              Max.   :39.71                   8      : 1  
##                                                              (Other): 0  
##  q21     q23    q26    q30    q41    q47    q49     q50     q52     q53    
##  1:  8   1:33   1:31   1:39   1:75   1:79   1:106   1:128   1:129   1:129  
##  2:112   2:96   2:98   2:90   2:26   2:16   2:  9   2:  1   2:  0   2:  0  
##  3:  2                        3:19   3:11   3:  7                          
##  4:  2                        4: 4   4: 6   4:  4                          
##  5:  2                        5: 4   5:12   5:  2                          
##  6:  3                        6: 1   6: 5   6:  1                          
##                               7: 0                                         
##  q63    q65    q87    q89   
##  2:79   2:67   1:40   1:42  
##  3:50   3:43   2:82   2:41  
##         4:19   3: 7   3:28  
##                       4: 9  
##                       5: 3  
##                       6: 2  
##                       7: 4

Variable Importance for every Local school districts

Broward County, FL (FT)

df_ft = df_fit_district %>% 
  filter(sitename == "Broward County, FL (FT)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_ft, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))
## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground
summary(model_lasso)
##             Length Class      Mode     
## a0          300    -none-     numeric  
## beta          3    -none-     list     
## dfmat       300    -none-     numeric  
## df          100    -none-     numeric  
## dim           2    -none-     numeric  
## lambda      100    -none-     numeric  
## dev.ratio   100    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##     alpha lambda
## 100     1   1000
model_lasso$results
##     alpha       lambda  Accuracy        Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.4925654 -0.056737581 0.17950185 0.34368644
## 2       1 1.149757e-03 0.4925654 -0.056737581 0.17950185 0.34368644
## 3       1 1.321941e-03 0.4925654 -0.051160153 0.17950185 0.33656961
## 4       1 1.519911e-03 0.4984477 -0.049544696 0.16862674 0.33446827
## 5       1 1.747528e-03 0.4984477 -0.049544696 0.16862674 0.33446827
## 6       1 2.009233e-03 0.5046977 -0.042205970 0.17734522 0.34743140
## 7       1 2.310130e-03 0.5164624 -0.026257737 0.17656366 0.34203839
## 8       1 2.656088e-03 0.5171569 -0.031176005 0.19175430 0.36128249
## 9       1 3.053856e-03 0.5227124 -0.032333214 0.18657151 0.35567429
## 10      1 3.511192e-03 0.5223856 -0.037707081 0.18591781 0.35239706
## 11      1 4.037017e-03 0.5216912 -0.058512931 0.17421325 0.33077623
## 12      1 4.641589e-03 0.5216912 -0.058512931 0.17421325 0.33077623
## 13      1 5.336699e-03 0.5220180 -0.064884544 0.17733092 0.33050426
## 14      1 6.135907e-03 0.5279003 -0.058390486 0.18071486 0.33342625
## 15      1 7.054802e-03 0.5396650 -0.051472700 0.16712265 0.32768303
## 16      1 8.111308e-03 0.5455474 -0.045313280 0.15832318 0.31923948
## 17      1 9.326033e-03 0.5514297 -0.039152596 0.15630865 0.31598590
## 18      1 1.072267e-02 0.5698121 -0.021502593 0.15766286 0.31698872
## 19      1 1.232847e-02 0.5812500 -0.014015091 0.16083783 0.32392801
## 20      1 1.417474e-02 0.5988971 -0.014635079 0.14685904 0.32592837
## 21      1 1.629751e-02 0.6033905 -0.034967158 0.11553317 0.26529635
## 22      1 1.873817e-02 0.6214052 -0.007976591 0.11249647 0.26600570
## 23      1 2.154435e-02 0.6265931 -0.011557564 0.09504912 0.22355406
## 24      1 2.477076e-02 0.6386846 -0.003333412 0.07786177 0.18889198
## 25      1 2.848036e-02 0.6445670  0.003615448 0.07056963 0.17997073
## 26      1 3.274549e-02 0.6560049  0.002183099 0.06642828 0.15338785
## 27      1 3.764936e-02 0.6615196 -0.012102991 0.05004643 0.11327282
## 28      1 4.328761e-02 0.6615196 -0.012102991 0.05004643 0.11327282
## 29      1 4.977024e-02 0.6674020 -0.002425572 0.05159105 0.10929945
## 30      1 5.722368e-02 0.6729167 -0.009677419 0.02477693 0.03060269
## 31      1 6.579332e-02 0.6787990  0.000000000 0.02493805 0.00000000
## 32      1 7.564633e-02 0.6787990  0.000000000 0.02493805 0.00000000
## 33      1 8.697490e-02 0.6787990  0.000000000 0.02493805 0.00000000
## 34      1 1.000000e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 35      1 1.149757e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 36      1 1.321941e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 37      1 1.519911e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 38      1 1.747528e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 39      1 2.009233e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 40      1 2.310130e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 41      1 2.656088e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 42      1 3.053856e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 43      1 3.511192e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 44      1 4.037017e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 45      1 4.641589e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 46      1 5.336699e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 47      1 6.135907e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 48      1 7.054802e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 49      1 8.111308e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 50      1 9.326033e-01 0.6787990  0.000000000 0.02493805 0.00000000
## 51      1 1.072267e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 52      1 1.232847e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 53      1 1.417474e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 54      1 1.629751e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 55      1 1.873817e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 56      1 2.154435e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 57      1 2.477076e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 58      1 2.848036e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 59      1 3.274549e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 60      1 3.764936e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 61      1 4.328761e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 62      1 4.977024e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 63      1 5.722368e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 64      1 6.579332e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 65      1 7.564633e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 66      1 8.697490e+00 0.6787990  0.000000000 0.02493805 0.00000000
## 67      1 1.000000e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 68      1 1.149757e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 69      1 1.321941e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 70      1 1.519911e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 71      1 1.747528e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 72      1 2.009233e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 73      1 2.310130e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 74      1 2.656088e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 75      1 3.053856e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 76      1 3.511192e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 77      1 4.037017e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 78      1 4.641589e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 79      1 5.336699e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 80      1 6.135907e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 81      1 7.054802e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 82      1 8.111308e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 83      1 9.326033e+01 0.6787990  0.000000000 0.02493805 0.00000000
## 84      1 1.072267e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 85      1 1.232847e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 86      1 1.417474e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 87      1 1.629751e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 88      1 1.873817e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 89      1 2.154435e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 90      1 2.477076e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 91      1 2.848036e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 92      1 3.274549e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 93      1 3.764936e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 94      1 4.328761e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 95      1 4.977024e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 96      1 5.722368e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 97      1 6.579332e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 98      1 7.564633e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 99      1 8.697490e+02 0.6787990  0.000000000 0.02493805 0.00000000
## 100     1 1.000000e+03 0.6787990  0.000000000 0.02493805 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  0.0  0.0  0.0
##          2 28.6 67.9  3.6
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.6786
varImp(model_lasso, scale = FALSE)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##        1 2 3
## q414   0 0 0
## race43 0 0 0
## race44 0 0 0
## age7   0 0 0
## q475   0 0 0
## sex2   0 0 0
## q492   0 0 0
## q895   0 0 0
## q493   0 0 0
## q174   0 0 0
## q262   0 0 0
## q413   0 0 0
## age5   0 0 0
## q173   0 0 0
## q872   0 0 0
## age4   0 0 0
## grade4 0 0 0
## q496   0 0 0
## q896   0 0 0
## q495   0 0 0
plot(varImp(model_lasso))
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 50) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 50) %>% 
  select(x2) %>% 
  arrange(desc(x2))
## [1] x2
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 50) %>% 
  select(x3) %>% 
  arrange(desc(x3))
## [1] x3
## <0 rows> (or 0-length row.names)

Chicago, IL (CH)

df_ch = df_fit_district %>% 
  filter(sitename == "Chicago, IL (CH)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_ch, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))

summary(model_lasso)
##             Length Class      Mode     
## a0          300    -none-     numeric  
## beta          3    -none-     list     
## dfmat       300    -none-     numeric  
## df          100    -none-     numeric  
## dim           2    -none-     numeric  
## lambda      100    -none-     numeric  
## dev.ratio   100    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha     lambda
## 31     1 0.06579332
model_lasso$results
##     alpha       lambda  Accuracy         Kappa AccuracySD   KappaSD
## 1       1 1.000000e-03 0.5095833  0.0498295022 0.07765491 0.1409245
## 2       1 1.149757e-03 0.5029167  0.0411669936 0.07166048 0.1338934
## 3       1 1.321941e-03 0.4966667  0.0247954863 0.06188899 0.1159450
## 4       1 1.519911e-03 0.4966667  0.0247954863 0.06188899 0.1159450
## 5       1 1.747528e-03 0.4966667  0.0201171237 0.06188899 0.1108077
## 6       1 2.009233e-03 0.5029167  0.0239347875 0.07748083 0.1356542
## 7       1 2.310130e-03 0.5154167  0.0418695702 0.09602537 0.1601455
## 8       1 2.656088e-03 0.5154167  0.0418695702 0.09602537 0.1601455
## 9       1 3.053856e-03 0.5091667  0.0359872172 0.09464440 0.1605335
## 10      1 3.511192e-03 0.5087500  0.0364604365 0.10609238 0.1768424
## 11      1 4.037017e-03 0.5216667  0.0510453332 0.11063788 0.1834184
## 12      1 4.641589e-03 0.5283333  0.0526524983 0.10895123 0.1787328
## 13      1 5.336699e-03 0.5408333  0.0698248123 0.11992474 0.2009180
## 14      1 6.135907e-03 0.5345833  0.0467826682 0.11663442 0.1941076
## 15      1 7.054802e-03 0.5408333  0.0431979256 0.09577897 0.1668315
## 16      1 8.111308e-03 0.5341667  0.0295586206 0.10818722 0.2021062
## 17      1 9.326033e-03 0.5408333  0.0335693157 0.09577897 0.1936579
## 18      1 1.072267e-02 0.5529167  0.0358402319 0.10465718 0.2073551
## 19      1 1.232847e-02 0.5720833  0.0561322054 0.10132323 0.2026559
## 20      1 1.417474e-02 0.5591667  0.0158992201 0.10529573 0.1930816
## 21      1 1.629751e-02 0.5650000  0.0005508962 0.10853258 0.1980581
## 22      1 1.873817e-02 0.5650000  0.0005508962 0.10853258 0.1980581
## 23      1 2.154435e-02 0.5716667  0.0111181932 0.11288173 0.2027024
## 24      1 2.477076e-02 0.5716667 -0.0023654789 0.11288173 0.2089249
## 25      1 2.848036e-02 0.5916667  0.0129387634 0.09930313 0.1880298
## 26      1 3.274549e-02 0.6045833  0.0208892864 0.06234394 0.1253779
## 27      1 3.764936e-02 0.6175000  0.0439131980 0.07045925 0.1345301
## 28      1 4.328761e-02 0.6245833  0.0460371262 0.05239076 0.1123193
## 29      1 4.977024e-02 0.6245833  0.0460371262 0.05239076 0.1123193
## 30      1 5.722368e-02 0.6370833  0.0677301063 0.05082081 0.1211111
## 31      1 6.579332e-02 0.6495833  0.0890787118 0.04553565 0.1158773
## 32      1 7.564633e-02 0.6429167  0.0631527859 0.03573792 0.1017076
## 33      1 8.697490e-02 0.6241667  0.0000000000 0.01819391 0.0000000
## 34      1 1.000000e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 35      1 1.149757e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 36      1 1.321941e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 37      1 1.519911e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 38      1 1.747528e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 39      1 2.009233e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 40      1 2.310130e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 41      1 2.656088e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 42      1 3.053856e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 43      1 3.511192e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 44      1 4.037017e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 45      1 4.641589e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 46      1 5.336699e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 47      1 6.135907e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 48      1 7.054802e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 49      1 8.111308e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 50      1 9.326033e-01 0.6241667  0.0000000000 0.01819391 0.0000000
## 51      1 1.072267e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 52      1 1.232847e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 53      1 1.417474e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 54      1 1.629751e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 55      1 1.873817e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 56      1 2.154435e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 57      1 2.477076e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 58      1 2.848036e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 59      1 3.274549e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 60      1 3.764936e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 61      1 4.328761e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 62      1 4.977024e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 63      1 5.722368e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 64      1 6.579332e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 65      1 7.564633e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 66      1 8.697490e+00 0.6241667  0.0000000000 0.01819391 0.0000000
## 67      1 1.000000e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 68      1 1.149757e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 69      1 1.321941e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 70      1 1.519911e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 71      1 1.747528e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 72      1 2.009233e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 73      1 2.310130e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 74      1 2.656088e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 75      1 3.053856e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 76      1 3.511192e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 77      1 4.037017e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 78      1 4.641589e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 79      1 5.336699e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 80      1 6.135907e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 81      1 7.054802e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 82      1 8.111308e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 83      1 9.326033e+01 0.6241667  0.0000000000 0.01819391 0.0000000
## 84      1 1.072267e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 85      1 1.232847e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 86      1 1.417474e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 87      1 1.629751e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 88      1 1.873817e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 89      1 2.154435e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 90      1 2.477076e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 91      1 2.848036e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 92      1 3.274549e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 93      1 3.764936e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 94      1 4.328761e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 95      1 4.977024e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 96      1 5.722368e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 97      1 6.579332e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 98      1 7.564633e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 99      1 8.697490e+02 0.6241667  0.0000000000 0.01819391 0.0000000
## 100     1 1.000000e+03 0.6241667  0.0000000000 0.01819391 0.0000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  2.5  0.0  0.0
##          2 28.0 62.4  7.0
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.6497
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##                 1     2 3
## q494      100.000  0.00 0
## q414        0.000 15.28 0
## age6        7.837  0.00 0
## age5        2.203  0.00 0
## q417        0.000  0.00 0
## bmi         0.000  0.00 0
## qnothhpl2   0.000  0.00 0
## grade2      0.000  0.00 0
## q493        0.000  0.00 0
## grade3      0.000  0.00 0
## q496        0.000  0.00 0
## q502        0.000  0.00 0
## q176        0.000  0.00 0
## q412        0.000  0.00 0
## q416        0.000  0.00 0
## age7        0.000  0.00 0
## q175        0.000  0.00 0
## q894        0.000  0.00 0
## age4        0.000  0.00 0
## race43      0.000  0.00 0
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##       x1
## q494 100
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##            x2
## q414 15.28388
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
## [1] x3
## <0 rows> (or 0-length row.names)

Eaton Consortium, MI (EA)

df_ea = df_fit_district %>% 
  filter(sitename == "Eaton Consortium, MI (EA)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_ea, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))
## Warning: from glmnet C++ code (error code -67); Convergence for 67th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -80); Convergence for 80th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -95); Convergence for 95th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -81); Convergence for 81th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -86); Convergence for 86th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -90); Convergence for 90th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
summary(model_lasso)
##             Length Class      Mode     
## a0          267    -none-     numeric  
## beta          3    -none-     list     
## dfmat       267    -none-     numeric  
## df           89    -none-     numeric  
## dim           2    -none-     numeric  
## lambda       89    -none-     numeric  
## dev.ratio    89    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##     alpha lambda
## 100     1   1000
model_lasso$results
##     alpha       lambda  Accuracy       Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.5814103 -0.07245085 0.08779165 0.18592575
## 2       1 1.149757e-03 0.5814103 -0.07245085 0.08779165 0.18592575
## 3       1 1.321941e-03 0.5974359 -0.04837678 0.09029137 0.21499156
## 4       1 1.519911e-03 0.5974359 -0.04837678 0.09029137 0.21499156
## 5       1 1.747528e-03 0.6057692 -0.04087678 0.10064307 0.21037305
## 6       1 2.009233e-03 0.6134615 -0.04416795 0.10433172 0.23293960
## 7       1 2.310130e-03 0.6057692 -0.07680865 0.09388345 0.18460487
## 8       1 2.656088e-03 0.5980769 -0.10123185 0.08903105 0.12656590
## 9       1 3.053856e-03 0.5980769 -0.10123185 0.08903105 0.12656590
## 10      1 3.511192e-03 0.5903846 -0.10656257 0.09765916 0.12756103
## 11      1 4.037017e-03 0.5903846 -0.12808643 0.11030485 0.11046953
## 12      1 4.641589e-03 0.5903846 -0.12808643 0.11030485 0.11046953
## 13      1 5.336699e-03 0.5987179 -0.12159556 0.10577247 0.10483643
## 14      1 6.135907e-03 0.6212454 -0.11740168 0.09422716 0.09119244
## 15      1 7.054802e-03 0.6135531 -0.13385737 0.11045591 0.11083977
## 16      1 8.111308e-03 0.6289377 -0.12137120 0.09680026 0.12692401
## 17      1 9.326033e-03 0.6360806 -0.11703085 0.08616519 0.12898942
## 18      1 1.072267e-02 0.6360806 -0.11005779 0.10662651 0.16347636
## 19      1 1.232847e-02 0.6437729 -0.10251427 0.10773649 0.16152592
## 20      1 1.417474e-02 0.6674908 -0.05092557 0.10586374 0.19443621
## 21      1 1.629751e-02 0.6674908 -0.05092557 0.10586374 0.19443621
## 22      1 1.873817e-02 0.6674908 -0.05092557 0.10586374 0.19443621
## 23      1 2.154435e-02 0.6752747 -0.03529854 0.11670562 0.19694302
## 24      1 2.477076e-02 0.6829670 -0.02779583 0.10896435 0.18924696
## 25      1 2.848036e-02 0.6823260 -0.06282398 0.08342938 0.14873974
## 26      1 3.274549e-02 0.6977106 -0.07063830 0.09139833 0.09511376
## 27      1 3.764936e-02 0.7451465 -0.02467339 0.05895687 0.05262862
## 28      1 4.328761e-02 0.7528388 -0.01403509 0.05625300 0.04438284
## 29      1 4.977024e-02 0.7528388 -0.01403509 0.05625300 0.04438284
## 30      1 5.722368e-02 0.7528388 -0.01403509 0.05625300 0.04438284
## 31      1 6.579332e-02 0.7528388 -0.01403509 0.05625300 0.04438284
## 32      1 7.564633e-02 0.7682234  0.00000000 0.02884397 0.00000000
## 33      1 8.697490e-02 0.7682234  0.00000000 0.02884397 0.00000000
## 34      1 1.000000e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 35      1 1.149757e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 36      1 1.321941e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 37      1 1.519911e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 38      1 1.747528e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 39      1 2.009233e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 40      1 2.310130e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 41      1 2.656088e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 42      1 3.053856e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 43      1 3.511192e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 44      1 4.037017e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 45      1 4.641589e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 46      1 5.336699e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 47      1 6.135907e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 48      1 7.054802e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 49      1 8.111308e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 50      1 9.326033e-01 0.7682234  0.00000000 0.02884397 0.00000000
## 51      1 1.072267e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 52      1 1.232847e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 53      1 1.417474e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 54      1 1.629751e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 55      1 1.873817e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 56      1 2.154435e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 57      1 2.477076e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 58      1 2.848036e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 59      1 3.274549e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 60      1 3.764936e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 61      1 4.328761e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 62      1 4.977024e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 63      1 5.722368e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 64      1 6.579332e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 65      1 7.564633e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 66      1 8.697490e+00 0.7682234  0.00000000 0.02884397 0.00000000
## 67      1 1.000000e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 68      1 1.149757e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 69      1 1.321941e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 70      1 1.519911e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 71      1 1.747528e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 72      1 2.009233e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 73      1 2.310130e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 74      1 2.656088e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 75      1 3.053856e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 76      1 3.511192e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 77      1 4.037017e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 78      1 4.641589e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 79      1 5.336699e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 80      1 6.135907e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 81      1 7.054802e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 82      1 8.111308e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 83      1 9.326033e+01 0.7682234  0.00000000 0.02884397 0.00000000
## 84      1 1.072267e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 85      1 1.232847e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 86      1 1.417474e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 87      1 1.629751e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 88      1 1.873817e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 89      1 2.154435e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 90      1 2.477076e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 91      1 2.848036e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 92      1 3.274549e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 93      1 3.764936e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 94      1 4.328761e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 95      1 4.977024e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 96      1 5.722368e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 97      1 6.579332e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 98      1 7.564633e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 99      1 8.697490e+02 0.7682234  0.00000000 0.02884397 0.00000000
## 100     1 1.000000e+03 0.7682234  0.00000000 0.02884397 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  0.0  0.0  0.0
##          2 16.3 76.7  7.0
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.7674
varImp(model_lasso, scale = FALSE)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##        1 2 3
## q414   0 0 0
## race43 0 0 0
## race44 0 0 0
## age7   0 0 0
## q475   0 0 0
## sex2   0 0 0
## q492   0 0 0
## q895   0 0 0
## q493   0 0 0
## q174   0 0 0
## q262   0 0 0
## q413   0 0 0
## age5   0 0 0
## q173   0 0 0
## q872   0 0 0
## age4   0 0 0
## grade4 0 0 0
## q496   0 0 0
## q896   0 0 0
## q495   0 0 0
plot(varImp(model_lasso))
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
## [1] x2
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
## [1] x3
## <0 rows> (or 0-length row.names)

Fort Worth, TX (FW)

df_fw = df_fit_district %>% 
  filter(sitename == "Fort Worth, TX (FW)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_fw, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))

summary(model_lasso)
##             Length Class      Mode     
## a0          279    -none-     numeric  
## beta          3    -none-     list     
## dfmat       279    -none-     numeric  
## df           93    -none-     numeric  
## dim           2    -none-     numeric  
## lambda       93    -none-     numeric  
## dev.ratio    93    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##     alpha lambda
## 100     1   1000
model_lasso$results
##     alpha       lambda  Accuracy        Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.6662688  0.088659183 0.04353769 0.16185524
## 2       1 1.149757e-03 0.6662688  0.088659183 0.04353769 0.16185524
## 3       1 1.321941e-03 0.6747833  0.099861540 0.03577065 0.15535385
## 4       1 1.519911e-03 0.6747833  0.099861540 0.03577065 0.15535385
## 5       1 1.747528e-03 0.6660711  0.062462888 0.04027119 0.15387385
## 6       1 2.009233e-03 0.6704190  0.065972928 0.04094547 0.14989115
## 7       1 2.310130e-03 0.6745856  0.071329427 0.04260750 0.17500844
## 8       1 2.656088e-03 0.6745856  0.054283640 0.04260750 0.18983757
## 9       1 3.053856e-03 0.6791311  0.057040660 0.04044663 0.18695364
## 10      1 3.511192e-03 0.6791311  0.057040660 0.04044663 0.18695364
## 11      1 4.037017e-03 0.6836456  0.053707375 0.04290291 0.19075310
## 12      1 4.641589e-03 0.6836456  0.053707375 0.04290291 0.19075310
## 13      1 5.336699e-03 0.6794789  0.035513572 0.04225732 0.17043718
## 14      1 6.135907e-03 0.6794789  0.026215353 0.03741495 0.15158904
## 15      1 7.054802e-03 0.6753123  0.009875484 0.03614303 0.13613222
## 16      1 8.111308e-03 0.6709644 -0.008487423 0.04104432 0.14268372
## 17      1 9.326033e-03 0.6751311  0.008290707 0.04264333 0.15027465
## 18      1 1.072267e-02 0.6879934  0.028069986 0.04632172 0.15523204
## 19      1 1.232847e-02 0.7007055  0.045425865 0.05119863 0.16202602
## 20      1 1.417474e-02 0.7048722  0.036119027 0.04979695 0.15275065
## 21      1 1.629751e-02 0.7048722  0.025729417 0.04979695 0.15115344
## 22      1 1.873817e-02 0.7048722  0.025729417 0.04979695 0.15115344
## 23      1 2.154435e-02 0.7048722 -0.002628811 0.03636384 0.10922225
## 24      1 2.477076e-02 0.7048722 -0.032343372 0.02346750 0.05193089
## 25      1 2.848036e-02 0.7090389 -0.027044579 0.01925045 0.04460088
## 26      1 3.274549e-02 0.7007055 -0.050725725 0.03283428 0.04849524
## 27      1 3.764936e-02 0.7007055 -0.050725725 0.03283428 0.04849524
## 28      1 4.328761e-02 0.7137655 -0.031487342 0.03775502 0.04438190
## 29      1 4.977024e-02 0.7220988 -0.018829114 0.04041644 0.04227609
## 30      1 5.722368e-02 0.7345988  0.000000000 0.02233381 0.00000000
## 31      1 6.579332e-02 0.7345988  0.000000000 0.02233381 0.00000000
## 32      1 7.564633e-02 0.7345988  0.000000000 0.02233381 0.00000000
## 33      1 8.697490e-02 0.7345988  0.000000000 0.02233381 0.00000000
## 34      1 1.000000e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 35      1 1.149757e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 36      1 1.321941e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 37      1 1.519911e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 38      1 1.747528e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 39      1 2.009233e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 40      1 2.310130e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 41      1 2.656088e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 42      1 3.053856e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 43      1 3.511192e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 44      1 4.037017e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 45      1 4.641589e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 46      1 5.336699e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 47      1 6.135907e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 48      1 7.054802e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 49      1 8.111308e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 50      1 9.326033e-01 0.7345988  0.000000000 0.02233381 0.00000000
## 51      1 1.072267e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 52      1 1.232847e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 53      1 1.417474e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 54      1 1.629751e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 55      1 1.873817e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 56      1 2.154435e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 57      1 2.477076e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 58      1 2.848036e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 59      1 3.274549e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 60      1 3.764936e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 61      1 4.328761e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 62      1 4.977024e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 63      1 5.722368e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 64      1 6.579332e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 65      1 7.564633e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 66      1 8.697490e+00 0.7345988  0.000000000 0.02233381 0.00000000
## 67      1 1.000000e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 68      1 1.149757e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 69      1 1.321941e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 70      1 1.519911e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 71      1 1.747528e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 72      1 2.009233e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 73      1 2.310130e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 74      1 2.656088e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 75      1 3.053856e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 76      1 3.511192e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 77      1 4.037017e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 78      1 4.641589e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 79      1 5.336699e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 80      1 6.135907e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 81      1 7.054802e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 82      1 8.111308e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 83      1 9.326033e+01 0.7345988  0.000000000 0.02233381 0.00000000
## 84      1 1.072267e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 85      1 1.232847e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 86      1 1.417474e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 87      1 1.629751e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 88      1 1.873817e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 89      1 2.154435e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 90      1 2.477076e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 91      1 2.848036e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 92      1 3.274549e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 93      1 3.764936e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 94      1 4.328761e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 95      1 4.977024e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 96      1 5.722368e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 97      1 6.579332e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 98      1 7.564633e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 99      1 8.697490e+02 0.7345988  0.000000000 0.02233381 0.00000000
## 100     1 1.000000e+03 0.7345988  0.000000000 0.02233381 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  0.0  0.0  0.0
##          2 19.0 73.4  7.6
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.7342
varImp(model_lasso, scale = FALSE)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##        1 2 3
## q414   0 0 0
## race43 0 0 0
## race44 0 0 0
## age7   0 0 0
## q475   0 0 0
## sex2   0 0 0
## q492   0 0 0
## q895   0 0 0
## q493   0 0 0
## q174   0 0 0
## q262   0 0 0
## q413   0 0 0
## age5   0 0 0
## q173   0 0 0
## q872   0 0 0
## age4   0 0 0
## grade4 0 0 0
## q496   0 0 0
## q896   0 0 0
## q495   0 0 0
plot(varImp(model_lasso))
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
## [1] x2
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
## [1] x3
## <0 rows> (or 0-length row.names)

Genesee Consortium, MI (GE)

df_ge = df_fit_district %>% 
  filter(sitename == "Genesee Consortium, MI (GE)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_ge, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))
## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground
summary(model_lasso)
##             Length Class      Mode     
## a0          300    -none-     numeric  
## beta          3    -none-     list     
## dfmat       300    -none-     numeric  
## df          100    -none-     numeric  
## dim           2    -none-     numeric  
## lambda      100    -none-     numeric  
## dev.ratio   100    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha     lambda
## 23     1 0.02154435
model_lasso$results
##     alpha       lambda  Accuracy        Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.6406618  0.109877494 0.08604027 0.19876689
## 2       1 1.149757e-03 0.6418137  0.120398707 0.11567561 0.23191382
## 3       1 1.321941e-03 0.6418137  0.120398707 0.11567561 0.23191382
## 4       1 1.519911e-03 0.6539461  0.132076445 0.12453041 0.23975486
## 5       1 1.747528e-03 0.6539461  0.132076445 0.12453041 0.23975486
## 6       1 2.009233e-03 0.6598284  0.137976445 0.12245358 0.24028674
## 7       1 2.310130e-03 0.6715931  0.149656231 0.11388317 0.23868310
## 8       1 2.656088e-03 0.6715931  0.139555220 0.11388317 0.22159776
## 9       1 3.053856e-03 0.6657108  0.133556632 0.12318270 0.23372563
## 10      1 3.511192e-03 0.6719608  0.140964039 0.12596520 0.24179511
## 11      1 4.037017e-03 0.6719608  0.130974723 0.12596520 0.25294136
## 12      1 4.641589e-03 0.6660784  0.099478475 0.12860314 0.26667735
## 13      1 5.336699e-03 0.6660784  0.099478475 0.12860314 0.26667735
## 14      1 6.135907e-03 0.6837255  0.119028983 0.12272902 0.26852736
## 15      1 7.054802e-03 0.6770588  0.074584538 0.11312213 0.24437723
## 16      1 8.111308e-03 0.6829412  0.096553769 0.10219430 0.21420492
## 17      1 9.326033e-03 0.6829412  0.088022443 0.10219430 0.22698200
## 18      1 1.072267e-02 0.6888235  0.095113042 0.09371647 0.22192348
## 19      1 1.232847e-02 0.6888235  0.095113042 0.09371647 0.22192348
## 20      1 1.417474e-02 0.6888235  0.095113042 0.09371647 0.22192348
## 21      1 1.629751e-02 0.7005882  0.112664027 0.08681648 0.21898420
## 22      1 1.873817e-02 0.7252206  0.180079250 0.10476102 0.30472510
## 23      1 2.154435e-02 0.7428676  0.198625045 0.08473025 0.27541555
## 24      1 2.477076e-02 0.7186029  0.087062502 0.06181920 0.18635112
## 25      1 2.848036e-02 0.7127206  0.077385083 0.06583248 0.19374100
## 26      1 3.274549e-02 0.7002206 -0.019941349 0.04853690 0.05998483
## 27      1 3.764936e-02 0.7064706 -0.009032258 0.05069301 0.08646124
## 28      1 4.328761e-02 0.7002206 -0.040143369 0.04853690 0.05198986
## 29      1 4.977024e-02 0.7119853 -0.020788530 0.03976341 0.04395616
## 30      1 5.722368e-02 0.7178676 -0.011111111 0.03283957 0.03513642
## 31      1 6.579332e-02 0.7241176  0.000000000 0.03236185 0.00000000
## 32      1 7.564633e-02 0.7241176  0.000000000 0.03236185 0.00000000
## 33      1 8.697490e-02 0.7241176  0.000000000 0.03236185 0.00000000
## 34      1 1.000000e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 35      1 1.149757e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 36      1 1.321941e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 37      1 1.519911e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 38      1 1.747528e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 39      1 2.009233e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 40      1 2.310130e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 41      1 2.656088e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 42      1 3.053856e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 43      1 3.511192e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 44      1 4.037017e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 45      1 4.641589e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 46      1 5.336699e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 47      1 6.135907e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 48      1 7.054802e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 49      1 8.111308e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 50      1 9.326033e-01 0.7241176  0.000000000 0.03236185 0.00000000
## 51      1 1.072267e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 52      1 1.232847e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 53      1 1.417474e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 54      1 1.629751e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 55      1 1.873817e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 56      1 2.154435e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 57      1 2.477076e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 58      1 2.848036e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 59      1 3.274549e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 60      1 3.764936e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 61      1 4.328761e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 62      1 4.977024e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 63      1 5.722368e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 64      1 6.579332e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 65      1 7.564633e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 66      1 8.697490e+00 0.7241176  0.000000000 0.03236185 0.00000000
## 67      1 1.000000e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 68      1 1.149757e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 69      1 1.321941e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 70      1 1.519911e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 71      1 1.747528e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 72      1 2.009233e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 73      1 2.310130e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 74      1 2.656088e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 75      1 3.053856e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 76      1 3.511192e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 77      1 4.037017e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 78      1 4.641589e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 79      1 5.336699e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 80      1 6.135907e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 81      1 7.054802e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 82      1 8.111308e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 83      1 9.326033e+01 0.7241176  0.000000000 0.03236185 0.00000000
## 84      1 1.072267e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 85      1 1.232847e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 86      1 1.417474e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 87      1 1.629751e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 88      1 1.873817e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 89      1 2.154435e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 90      1 2.477076e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 91      1 2.848036e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 92      1 3.274549e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 93      1 3.764936e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 94      1 4.328761e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 95      1 4.977024e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 96      1 5.722368e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 97      1 6.579332e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 98      1 7.564633e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 99      1 8.697490e+02 0.7241176  0.000000000 0.03236185 0.00000000
## 100     1 1.000000e+03 0.7241176  0.000000000 0.03236185 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  6.0  4.2  0.6
##          2 16.9 68.1  4.2
##          3  0.0  0.0  0.0
##                            
##  Accuracy (average) : 0.741
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##                1     2     3
## q177      100.00  0.00  0.00
## q532       89.72  0.00  0.00
## q496        0.00 72.61  0.00
## q415        0.00 72.05  0.00
## q213       72.04  0.00  0.00
## q215       67.37  0.00  0.00
## q494       18.93  0.00 43.78
## q175        0.00 42.66  0.00
## q894        0.00 39.12  0.00
## q872       35.14  0.00  0.00
## q654       34.62  0.00  0.00
## q414        0.00 33.77  0.00
## qnothhpl2   0.00 32.85  0.00
## q412        0.00 32.56  0.00
## q232       31.05  0.00  0.00
## q493        0.00 27.81  0.00
## q897        0.00 27.60  0.00
## q474        0.00 25.29  0.00
## q492        0.00 25.03  0.00
## q174        0.00  0.00 22.79
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##               x1
## q177   100.00000
## q532    89.72323
## q213    72.04383
## q215    67.36966
## q872    35.13896
## q654    34.61790
## q232    31.04861
## q494    18.93238
## q172    18.33631
## grade2  16.35997
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##                 x2
## q496      72.61062
## q415      72.04780
## q175      42.66361
## q894      39.12014
## q414      33.77327
## qnothhpl2 32.85212
## q412      32.56322
## q493      27.81219
## q897      27.59926
## q474      25.28549
## q492      25.02565
## q472      21.73536
## q522      18.97933
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
##            x3
## q494 43.77922
## q174 22.78910

Hillsborough County, FL (HL)

df_hl = df_fit_district %>% 
  filter(sitename == "Hillsborough County, FL (HL)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_hl, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))

summary(model_lasso)
##             Length Class      Mode     
## a0          288    -none-     numeric  
## beta          3    -none-     list     
## dfmat       288    -none-     numeric  
## df           96    -none-     numeric  
## dim           2    -none-     numeric  
## lambda       96    -none-     numeric  
## dev.ratio    96    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##     alpha lambda
## 100     1   1000
model_lasso$results
##     alpha       lambda  Accuracy        Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.6975764  0.075786212 0.06855044 0.13990379
## 2       1 1.149757e-03 0.7017430  0.082529138 0.07578905 0.14821735
## 3       1 1.321941e-03 0.7097559  0.081922099 0.07237873 0.14656233
## 4       1 1.519911e-03 0.7097559  0.081922099 0.07237873 0.14656233
## 5       1 1.747528e-03 0.7137559  0.099779241 0.06650619 0.12966266
## 6       1 2.009233e-03 0.7219225  0.105008188 0.07106512 0.12710278
## 7       1 2.310130e-03 0.7222430  0.107025917 0.07657302 0.13044676
## 8       1 2.656088e-03 0.7222430  0.107025917 0.07657302 0.13044676
## 9       1 3.053856e-03 0.7427977  0.132796874 0.06941435 0.13084309
## 10      1 3.511192e-03 0.7509643  0.149662977 0.07077582 0.14590194
## 11      1 4.037017e-03 0.7589771  0.160709577 0.06675436 0.14290446
## 12      1 4.641589e-03 0.7551310  0.140549150 0.07192981 0.17100400
## 13      1 5.336699e-03 0.7509643  0.107186054 0.07077582 0.18450883
## 14      1 6.135907e-03 0.7549643  0.110927252 0.06995697 0.18031344
## 15      1 7.054802e-03 0.7512993  0.062099342 0.06667684 0.18471450
## 16      1 8.111308e-03 0.7512993  0.062099342 0.06667684 0.18471450
## 17      1 9.326033e-03 0.7594933  0.074705662 0.06980430 0.18764402
## 18      1 1.072267e-02 0.7591455  0.046710312 0.05962167 0.17948889
## 19      1 1.232847e-02 0.7708378  0.062030160 0.04542368 0.17881947
## 20      1 1.417474e-02 0.7745173  0.050081423 0.02969109 0.13715536
## 21      1 1.629751e-02 0.7746711  0.050866517 0.03832167 0.14763651
## 22      1 1.873817e-02 0.7709916  0.007460833 0.04435393 0.13105877
## 23      1 2.154435e-02 0.7748378  0.012285178 0.03798273 0.12723463
## 24      1 2.477076e-02 0.7791856 -0.003928376 0.03675934 0.07762052
## 25      1 2.848036e-02 0.7750190 -0.030595043 0.03754058 0.03242548
## 26      1 3.274549e-02 0.7791856 -0.023928376 0.03675934 0.03100745
## 27      1 3.764936e-02 0.7911984 -0.005813953 0.02951882 0.01838534
## 28      1 4.328761e-02 0.7950446  0.000000000 0.02242322 0.00000000
## 29      1 4.977024e-02 0.7950446  0.000000000 0.02242322 0.00000000
## 30      1 5.722368e-02 0.7950446  0.000000000 0.02242322 0.00000000
## 31      1 6.579332e-02 0.7950446  0.000000000 0.02242322 0.00000000
## 32      1 7.564633e-02 0.7950446  0.000000000 0.02242322 0.00000000
## 33      1 8.697490e-02 0.7950446  0.000000000 0.02242322 0.00000000
## 34      1 1.000000e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 35      1 1.149757e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 36      1 1.321941e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 37      1 1.519911e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 38      1 1.747528e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 39      1 2.009233e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 40      1 2.310130e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 41      1 2.656088e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 42      1 3.053856e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 43      1 3.511192e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 44      1 4.037017e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 45      1 4.641589e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 46      1 5.336699e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 47      1 6.135907e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 48      1 7.054802e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 49      1 8.111308e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 50      1 9.326033e-01 0.7950446  0.000000000 0.02242322 0.00000000
## 51      1 1.072267e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 52      1 1.232847e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 53      1 1.417474e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 54      1 1.629751e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 55      1 1.873817e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 56      1 2.154435e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 57      1 2.477076e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 58      1 2.848036e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 59      1 3.274549e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 60      1 3.764936e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 61      1 4.328761e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 62      1 4.977024e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 63      1 5.722368e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 64      1 6.579332e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 65      1 7.564633e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 66      1 8.697490e+00 0.7950446  0.000000000 0.02242322 0.00000000
## 67      1 1.000000e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 68      1 1.149757e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 69      1 1.321941e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 70      1 1.519911e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 71      1 1.747528e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 72      1 2.009233e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 73      1 2.310130e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 74      1 2.656088e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 75      1 3.053856e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 76      1 3.511192e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 77      1 4.037017e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 78      1 4.641589e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 79      1 5.336699e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 80      1 6.135907e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 81      1 7.054802e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 82      1 8.111308e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 83      1 9.326033e+01 0.7950446  0.000000000 0.02242322 0.00000000
## 84      1 1.072267e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 85      1 1.232847e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 86      1 1.417474e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 87      1 1.629751e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 88      1 1.873817e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 89      1 2.154435e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 90      1 2.477076e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 91      1 2.848036e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 92      1 3.274549e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 93      1 3.764936e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 94      1 4.328761e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 95      1 4.977024e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 96      1 5.722368e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 97      1 6.579332e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 98      1 7.564633e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 99      1 8.697490e+02 0.7950446  0.000000000 0.02242322 0.00000000
## 100     1 1.000000e+03 0.7950446  0.000000000 0.02242322 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  0.0  0.0  0.0
##          2 14.9 79.4  5.6
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.7944
varImp(model_lasso, scale = FALSE)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##        1 2 3
## q414   0 0 0
## race43 0 0 0
## race44 0 0 0
## age7   0 0 0
## q475   0 0 0
## sex2   0 0 0
## q492   0 0 0
## q895   0 0 0
## q493   0 0 0
## q174   0 0 0
## q262   0 0 0
## q413   0 0 0
## age5   0 0 0
## q173   0 0 0
## q872   0 0 0
## age4   0 0 0
## grade4 0 0 0
## q496   0 0 0
## q896   0 0 0
## q495   0 0 0
plot(varImp(model_lasso))
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 50) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 50) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 50) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)

Los Angeles, CA (LO)

df_lo = df_fit_district %>% 
  filter(sitename == "Los Angeles, CA (LO)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_lo, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))
## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground
## Warning: from glmnet C++ code (error code -74); Convergence for 74th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground
## Warning: from glmnet C++ code (error code -89); Convergence for 89th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground

## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground
## Warning: from glmnet C++ code (error code -93); Convergence for 93th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning in lognet(xd, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one
## multinomial or binomial class has fewer than 8 observations; dangerous ground
summary(model_lasso)
##             Length Class      Mode     
## a0          294    -none-     numeric  
## beta          3    -none-     list     
## dfmat       294    -none-     numeric  
## df           98    -none-     numeric  
## dim           2    -none-     numeric  
## lambda       98    -none-     numeric  
## dev.ratio    98    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##     alpha lambda
## 100     1   1000
model_lasso$results
##     alpha       lambda  Accuracy        Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.5975253  0.024156821 0.12138326 0.22412200
## 2       1 1.149757e-03 0.5975253  0.010360834 0.12138326 0.23375900
## 3       1 1.321941e-03 0.5884343 -0.006518961 0.10536904 0.19573045
## 4       1 1.519911e-03 0.5975253  0.008701834 0.09604298 0.19859885
## 5       1 1.747528e-03 0.5975253  0.008701834 0.09604298 0.19859885
## 6       1 2.009233e-03 0.5875253  0.006439391 0.08914616 0.20077332
## 7       1 2.310130e-03 0.5882828  0.002346657 0.09414878 0.20014573
## 8       1 2.656088e-03 0.5791919 -0.007277756 0.10274073 0.19357220
## 9       1 3.053856e-03 0.5625253 -0.039942640 0.12754007 0.17798859
## 10      1 3.511192e-03 0.5625253 -0.039942640 0.12754007 0.17798859
## 11      1 4.037017e-03 0.5716162 -0.035519504 0.12211268 0.17737675
## 12      1 4.641589e-03 0.5716162 -0.035519504 0.12211268 0.17737675
## 13      1 5.336699e-03 0.5525253 -0.066581365 0.13516747 0.18427779
## 14      1 6.135907e-03 0.5699495 -0.039822727 0.13818078 0.23447649
## 15      1 7.054802e-03 0.5775253 -0.036518979 0.11109868 0.19232054
## 16      1 8.111308e-03 0.5875253 -0.046278250 0.10779672 0.19466757
## 17      1 9.326033e-03 0.5875253 -0.041375288 0.12366579 0.21346262
## 18      1 1.072267e-02 0.5958586 -0.031259376 0.11986173 0.22192100
## 19      1 1.232847e-02 0.6041919 -0.050231173 0.09653852 0.21918039
## 20      1 1.417474e-02 0.6041919 -0.050231173 0.09653852 0.21918039
## 21      1 1.629751e-02 0.6041919 -0.043202065 0.11398588 0.26307528
## 22      1 1.873817e-02 0.6327273 -0.008594127 0.10992062 0.27491103
## 23      1 2.154435e-02 0.6327273 -0.053408197 0.10122250 0.22747642
## 24      1 2.477076e-02 0.6609091 -0.021666376 0.07887460 0.22076328
## 25      1 2.848036e-02 0.6965152  0.069974364 0.11033718 0.31657004
## 26      1 3.274549e-02 0.6874242  0.027869101 0.10267827 0.29171166
## 27      1 3.764936e-02 0.6874242 -0.004834453 0.08288385 0.25014727
## 28      1 4.328761e-02 0.6981818  0.002854394 0.08902022 0.21656244
## 29      1 4.977024e-02 0.6974242 -0.026043322 0.07115317 0.15658270
## 30      1 5.722368e-02 0.6890909 -0.058118794 0.06916353 0.10043477
## 31      1 6.579332e-02 0.7163636 -0.017391304 0.04098571 0.05499613
## 32      1 7.564633e-02 0.7274747  0.000000000 0.04107689 0.00000000
## 33      1 8.697490e-02 0.7274747  0.000000000 0.04107689 0.00000000
## 34      1 1.000000e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 35      1 1.149757e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 36      1 1.321941e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 37      1 1.519911e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 38      1 1.747528e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 39      1 2.009233e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 40      1 2.310130e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 41      1 2.656088e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 42      1 3.053856e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 43      1 3.511192e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 44      1 4.037017e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 45      1 4.641589e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 46      1 5.336699e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 47      1 6.135907e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 48      1 7.054802e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 49      1 8.111308e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 50      1 9.326033e-01 0.7274747  0.000000000 0.04107689 0.00000000
## 51      1 1.072267e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 52      1 1.232847e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 53      1 1.417474e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 54      1 1.629751e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 55      1 1.873817e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 56      1 2.154435e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 57      1 2.477076e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 58      1 2.848036e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 59      1 3.274549e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 60      1 3.764936e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 61      1 4.328761e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 62      1 4.977024e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 63      1 5.722368e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 64      1 6.579332e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 65      1 7.564633e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 66      1 8.697490e+00 0.7274747  0.000000000 0.04107689 0.00000000
## 67      1 1.000000e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 68      1 1.149757e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 69      1 1.321941e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 70      1 1.519911e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 71      1 1.747528e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 72      1 2.009233e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 73      1 2.310130e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 74      1 2.656088e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 75      1 3.053856e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 76      1 3.511192e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 77      1 4.037017e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 78      1 4.641589e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 79      1 5.336699e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 80      1 6.135907e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 81      1 7.054802e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 82      1 8.111308e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 83      1 9.326033e+01 0.7274747  0.000000000 0.04107689 0.00000000
## 84      1 1.072267e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 85      1 1.232847e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 86      1 1.417474e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 87      1 1.629751e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 88      1 1.873817e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 89      1 2.154435e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 90      1 2.477076e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 91      1 2.848036e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 92      1 3.274549e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 93      1 3.764936e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 94      1 4.328761e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 95      1 4.977024e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 96      1 5.722368e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 97      1 6.579332e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 98      1 7.564633e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 99      1 8.697490e+02 0.7274747  0.000000000 0.04107689 0.00000000
## 100     1 1.000000e+03 0.7274747  0.000000000 0.04107689 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  0.0  0.0  0.0
##          2 23.9 72.5  3.7
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.7248
varImp(model_lasso, scale = FALSE)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##        1 2 3
## q414   0 0 0
## race43 0 0 0
## race44 0 0 0
## age7   0 0 0
## q475   0 0 0
## sex2   0 0 0
## q492   0 0 0
## q895   0 0 0
## q493   0 0 0
## q174   0 0 0
## q262   0 0 0
## q413   0 0 0
## age5   0 0 0
## q173   0 0 0
## q872   0 0 0
## age4   0 0 0
## grade4 0 0 0
## q496   0 0 0
## q896   0 0 0
## q495   0 0 0
plot(varImp(model_lasso))
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 50) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 50) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 50) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)

Newark, NJ (NW)

df_nw = df_fit_district %>% 
  filter(sitename == "Newark, NJ (NW)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_nw, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))
## Warning: from glmnet C++ code (error code -68); Convergence for 68th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -84); Convergence for 84th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
summary(model_lasso)
##             Length Class      Mode     
## a0          300    -none-     numeric  
## beta          3    -none-     list     
## dfmat       300    -none-     numeric  
## df          100    -none-     numeric  
## dim           2    -none-     numeric  
## lambda      100    -none-     numeric  
## dev.ratio   100    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha    lambda
## 33     1 0.0869749
model_lasso$results
##     alpha       lambda  Accuracy        Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.5051282  0.057279202 0.08483578 0.15958556
## 2       1 1.149757e-03 0.5051282  0.062006093 0.08483578 0.16478058
## 3       1 1.321941e-03 0.4979853  0.046300964 0.08157773 0.14811992
## 4       1 1.519911e-03 0.4908425  0.025870857 0.08445799 0.16797464
## 5       1 1.747528e-03 0.4979853  0.038605688 0.08825369 0.18522478
## 6       1 2.009233e-03 0.4979853  0.028205358 0.08825369 0.19104528
## 7       1 2.310130e-03 0.4979853  0.025047463 0.08825369 0.19182271
## 8       1 2.656088e-03 0.4984615  0.014793331 0.09061652 0.18637016
## 9       1 3.053856e-03 0.4841026 -0.001375342 0.11197469 0.20006649
## 10      1 3.511192e-03 0.4769597 -0.021537787 0.10799263 0.21537933
## 11      1 4.037017e-03 0.4769597 -0.031633941 0.10799263 0.22731127
## 12      1 4.641589e-03 0.4841026 -0.007957471 0.11197469 0.21703071
## 13      1 5.336699e-03 0.4912454 -0.003389490 0.12014434 0.22723143
## 14      1 6.135907e-03 0.4912454  0.002100707 0.12014434 0.22829900
## 15      1 7.054802e-03 0.4912454 -0.008315960 0.12014434 0.21112455
## 16      1 8.111308e-03 0.4917216 -0.015975699 0.11841839 0.19210349
## 17      1 9.326033e-03 0.4850549 -0.040092428 0.11341620 0.18081015
## 18      1 1.072267e-02 0.4783883 -0.064370569 0.11221606 0.18428595
## 19      1 1.232847e-02 0.4921978 -0.052249317 0.12142672 0.17658552
## 20      1 1.417474e-02 0.5064835 -0.042006121 0.12609190 0.18384246
## 21      1 1.629751e-02 0.4931502 -0.070486794 0.13731971 0.20769505
## 22      1 1.873817e-02 0.5064835 -0.051395885 0.14088926 0.21427894
## 23      1 2.154435e-02 0.5207692 -0.031283103 0.14335616 0.21521177
## 24      1 2.477076e-02 0.5402930 -0.004807241 0.12470111 0.20648582
## 25      1 2.848036e-02 0.5541026  0.016915448 0.13278488 0.22074964
## 26      1 3.274549e-02 0.5760806  0.048069829 0.11870331 0.19565182
## 27      1 3.764936e-02 0.5694139  0.009801146 0.11047737 0.16690627
## 28      1 4.328761e-02 0.5698901 -0.010225845 0.10888946 0.14755463
## 29      1 4.977024e-02 0.5755311 -0.020116391 0.11219464 0.18255691
## 30      1 5.722368e-02 0.6047253  0.014184571 0.07405439 0.12401023
## 31      1 6.579332e-02 0.6267033  0.039936914 0.04582724 0.10872151
## 32      1 7.564633e-02 0.6272527  0.027647059 0.02062892 0.06100844
## 33      1 8.697490e-02 0.6343956  0.035333333 0.03440124 0.08279381
## 34      1 1.000000e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 35      1 1.149757e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 36      1 1.321941e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 37      1 1.519911e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 38      1 1.747528e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 39      1 2.009233e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 40      1 2.310130e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 41      1 2.656088e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 42      1 3.053856e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 43      1 3.511192e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 44      1 4.037017e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 45      1 4.641589e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 46      1 5.336699e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 47      1 6.135907e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 48      1 7.054802e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 49      1 8.111308e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 50      1 9.326033e-01 0.6272527  0.000000000 0.02062892 0.00000000
## 51      1 1.072267e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 52      1 1.232847e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 53      1 1.417474e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 54      1 1.629751e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 55      1 1.873817e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 56      1 2.154435e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 57      1 2.477076e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 58      1 2.848036e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 59      1 3.274549e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 60      1 3.764936e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 61      1 4.328761e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 62      1 4.977024e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 63      1 5.722368e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 64      1 6.579332e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 65      1 7.564633e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 66      1 8.697490e+00 0.6272527  0.000000000 0.02062892 0.00000000
## 67      1 1.000000e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 68      1 1.149757e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 69      1 1.321941e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 70      1 1.519911e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 71      1 1.747528e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 72      1 2.009233e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 73      1 2.310130e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 74      1 2.656088e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 75      1 3.053856e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 76      1 3.511192e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 77      1 4.037017e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 78      1 4.641589e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 79      1 5.336699e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 80      1 6.135907e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 81      1 7.054802e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 82      1 8.111308e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 83      1 9.326033e+01 0.6272527  0.000000000 0.02062892 0.00000000
## 84      1 1.072267e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 85      1 1.232847e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 86      1 1.417474e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 87      1 1.629751e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 88      1 1.873817e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 89      1 2.154435e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 90      1 2.477076e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 91      1 2.848036e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 92      1 3.274549e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 93      1 3.764936e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 94      1 4.328761e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 95      1 4.977024e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 96      1 5.722368e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 97      1 6.579332e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 98      1 7.564633e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 99      1 8.697490e+02 0.6272527  0.000000000 0.02062892 0.00000000
## 100     1 1.000000e+03 0.6272527  0.000000000 0.02062892 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  1.4  0.7  0.0
##          2 26.8 62.0  9.2
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.6338
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##               1   2     3
## q476       0.00 100  0.00
## q873       0.00   0 82.23
## qnothhpl2 23.34   0  0.00
## q494      10.09   0  0.00
## q415       0.00   0  0.00
## race43     0.00   0  0.00
## race44     0.00   0  0.00
## age7       0.00   0  0.00
## q492       0.00   0  0.00
## sex2       0.00   0  0.00
## q495       0.00   0  0.00
## q496       0.00   0  0.00
## q175       0.00   0  0.00
## q302       0.00   0  0.00
## q414       0.00   0  0.00
## age5       0.00   0  0.00
## q174       0.00   0  0.00
## q894       0.00   0  0.00
## age4       0.00   0  0.00
## grade4     0.00   0  0.00
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##                 x1
## qnothhpl2 23.34384
## q494      10.09149
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##       x2
## q476 100
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
##            x3
## q873 82.23128

Orange County, FL (OL)

df_ol = df_fit_district %>% 
  filter(sitename == "Orange County, FL (OL)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_ol, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))
## Warning: from glmnet C++ code (error code -66); Convergence for 66th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -76); Convergence for 76th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -93); Convergence for 93th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -66); Convergence for 66th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -88); Convergence for 88th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -60); Convergence for 60th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
summary(model_lasso)
##             Length Class      Mode     
## a0          177    -none-     numeric  
## beta          3    -none-     list     
## dfmat       177    -none-     numeric  
## df           59    -none-     numeric  
## dim           2    -none-     numeric  
## lambda       59    -none-     numeric  
## dev.ratio    59    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha     lambda
## 27     1 0.03764936
model_lasso$results
##     alpha       lambda  Accuracy        Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.5660839 -0.010478878 0.14137336 0.27594528
## 2       1 1.149757e-03 0.5577506 -0.013928903 0.15312694 0.27810025
## 3       1 1.321941e-03 0.5654429  0.008994377 0.16229509 0.28696859
## 4       1 1.519911e-03 0.5577506 -0.029062055 0.14800219 0.25508458
## 5       1 1.747528e-03 0.5744172 -0.012562055 0.15014498 0.25352104
## 6       1 2.009233e-03 0.5744172 -0.028189962 0.15014498 0.25473481
## 7       1 2.310130e-03 0.5813520 -0.020585330 0.14263023 0.25181588
## 8       1 2.656088e-03 0.6071096  0.034244724 0.15559230 0.28873792
## 9       1 3.053856e-03 0.6154429  0.035176350 0.14620768 0.29080810
## 10      1 3.511192e-03 0.6245338  0.039497911 0.14418470 0.28505579
## 11      1 4.037017e-03 0.6336247  0.043528534 0.14783322 0.27012636
## 12      1 4.641589e-03 0.6245338  0.035099415 0.14418470 0.27762877
## 13      1 5.336699e-03 0.6328671  0.026707118 0.13279413 0.25689615
## 14      1 6.135907e-03 0.6495338  0.039217142 0.12446306 0.24516897
## 15      1 7.054802e-03 0.6669580  0.053930117 0.11074825 0.23361629
## 16      1 8.111308e-03 0.6663170  0.056674038 0.12611108 0.24027233
## 17      1 9.326033e-03 0.6983683  0.092889477 0.11009590 0.26069831
## 18      1 1.072267e-02 0.7074592  0.145571469 0.11632489 0.26209755
## 19      1 1.232847e-02 0.7234848  0.152457997 0.10502821 0.22961568
## 20      1 1.417474e-02 0.7234848  0.152457997 0.10502821 0.22961568
## 21      1 1.629751e-02 0.7241259  0.128071469 0.09530034 0.21321084
## 22      1 1.873817e-02 0.7331002  0.139725889 0.06053567 0.17696104
## 23      1 2.154435e-02 0.7407925  0.153734510 0.05965714 0.18645334
## 24      1 2.477076e-02 0.7407925  0.121176370 0.04489727 0.14667727
## 25      1 2.848036e-02 0.7498834  0.106949348 0.06637492 0.20559747
## 26      1 3.274549e-02 0.7498834  0.106949348 0.06637492 0.20559747
## 27      1 3.764936e-02 0.7582168  0.119159875 0.05965755 0.21247039
## 28      1 4.328761e-02 0.7414336  0.017361702 0.03981897 0.09815127
## 29      1 4.977024e-02 0.7497669  0.033547749 0.04938251 0.14731052
## 30      1 5.722368e-02 0.7574592  0.044186047 0.04525638 0.13972855
## 31      1 6.579332e-02 0.7574592  0.044186047 0.04525638 0.13972855
## 32      1 7.564633e-02 0.7491259  0.000000000 0.03657195 0.00000000
## 33      1 8.697490e-02 0.7491259  0.000000000 0.03657195 0.00000000
## 34      1 1.000000e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 35      1 1.149757e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 36      1 1.321941e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 37      1 1.519911e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 38      1 1.747528e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 39      1 2.009233e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 40      1 2.310130e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 41      1 2.656088e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 42      1 3.053856e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 43      1 3.511192e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 44      1 4.037017e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 45      1 4.641589e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 46      1 5.336699e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 47      1 6.135907e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 48      1 7.054802e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 49      1 8.111308e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 50      1 9.326033e-01 0.7491259  0.000000000 0.03657195 0.00000000
## 51      1 1.072267e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 52      1 1.232847e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 53      1 1.417474e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 54      1 1.629751e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 55      1 1.873817e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 56      1 2.154435e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 57      1 2.477076e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 58      1 2.848036e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 59      1 3.274549e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 60      1 3.764936e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 61      1 4.328761e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 62      1 4.977024e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 63      1 5.722368e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 64      1 6.579332e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 65      1 7.564633e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 66      1 8.697490e+00 0.7491259  0.000000000 0.03657195 0.00000000
## 67      1 1.000000e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 68      1 1.149757e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 69      1 1.321941e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 70      1 1.519911e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 71      1 1.747528e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 72      1 2.009233e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 73      1 2.310130e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 74      1 2.656088e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 75      1 3.053856e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 76      1 3.511192e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 77      1 4.037017e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 78      1 4.641589e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 79      1 5.336699e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 80      1 6.135907e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 81      1 7.054802e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 82      1 8.111308e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 83      1 9.326033e+01 0.7491259  0.000000000 0.03657195 0.00000000
## 84      1 1.072267e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 85      1 1.232847e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 86      1 1.417474e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 87      1 1.629751e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 88      1 1.873817e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 89      1 2.154435e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 90      1 2.477076e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 91      1 2.848036e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 92      1 3.274549e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 93      1 3.764936e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 94      1 4.328761e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 95      1 4.977024e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 96      1 5.722368e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 97      1 6.579332e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 98      1 7.564633e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 99      1 8.697490e+02 0.7491259  0.000000000 0.03657195 0.00000000
## 100     1 1.000000e+03 0.7491259  0.000000000 0.03657195 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  2.4  1.6  0.0
##          2 15.4 73.2  7.3
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.7561
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##                1       2       3
## q896       0.000  0.0000 100.000
## q213       0.000 76.1176   0.000
## qnothhpl2 36.466  0.0000   0.000
## q474       0.000 35.6283  21.831
## q895      34.018  0.0000   0.000
## q654      25.246  0.0000   0.000
## q475       0.000 24.8464   0.000
## q633      22.425  0.0000   0.000
## q872       0.000  0.0000  16.352
## q496       0.000 16.3330   0.000
## age6      13.307  0.0000   0.000
## race43     0.000 11.2153   0.000
## q492       0.000  9.6719   0.000
## q494       8.223  0.0000   0.000
## grade3     0.000  6.3507   0.000
## q473       0.000  5.2787   0.000
## grade4     0.000  4.4407   0.000
## q173       0.000  3.4226   0.000
## q893       0.000  0.0000   1.037
## bmi        0.000  0.9664   0.000
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##                 x1
## qnothhpl2 36.46561
## q895      34.01832
## q654      25.24576
## q633      22.42473
## age6      13.30703
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##              x2
## q213   76.11757
## q474   35.62827
## q475   24.84641
## q496   16.33300
## race43 11.21535
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
##             x3
## q896 100.00000
## q474  21.83091
## q872  16.35161

Palm Beach County, FL (PB)

df_ol = df_fit_district %>% 
  filter(sitename == "Palm Beach County, FL (PB)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_ol, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))

summary(model_lasso)
##             Length Class      Mode     
## a0          291    -none-     numeric  
## beta          3    -none-     list     
## dfmat       291    -none-     numeric  
## df           97    -none-     numeric  
## dim           2    -none-     numeric  
## lambda       97    -none-     numeric  
## dev.ratio    97    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha     lambda
## 23     1 0.02154435
model_lasso$results
##     alpha       lambda  Accuracy      Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.6781656 0.12466065 0.05315993 0.14478135
## 2       1 1.149757e-03 0.6781656 0.12466065 0.05315993 0.14478135
## 3       1 1.321941e-03 0.6849472 0.13248699 0.04484440 0.13515980
## 4       1 1.519911e-03 0.6915204 0.14520388 0.04214489 0.11956877
## 5       1 1.747528e-03 0.6947462 0.14954701 0.03934369 0.11774691
## 6       1 2.009233e-03 0.6978712 0.14914749 0.04257082 0.12771251
## 7       1 2.310130e-03 0.7009962 0.15136609 0.04766483 0.13169304
## 8       1 2.656088e-03 0.7011038 0.14216390 0.05169750 0.14570357
## 9       1 3.053856e-03 0.7011038 0.14216390 0.05169750 0.14570357
## 10      1 3.511192e-03 0.6979788 0.11538896 0.04147155 0.11254715
## 11      1 4.037017e-03 0.6981871 0.11537036 0.03931563 0.11443590
## 12      1 4.641589e-03 0.6983020 0.10728954 0.03841299 0.09328565
## 13      1 5.336699e-03 0.6985245 0.10199027 0.04798077 0.13129336
## 14      1 6.135907e-03 0.7018579 0.10995068 0.04666135 0.13402528
## 15      1 7.054802e-03 0.7083162 0.11936701 0.04936138 0.14249934
## 16      1 8.111308e-03 0.7181011 0.14441490 0.03855607 0.10829246
## 17      1 9.326033e-03 0.7179936 0.12334083 0.03959914 0.12807581
## 18      1 1.072267e-02 0.7216502 0.11977766 0.04069483 0.12653248
## 19      1 1.232847e-02 0.7317652 0.13784677 0.05374897 0.15281810
## 20      1 1.417474e-02 0.7348902 0.14232966 0.04962053 0.14958394
## 21      1 1.629751e-02 0.7481160 0.16295730 0.04239996 0.13641110
## 22      1 1.873817e-02 0.7477927 0.15017081 0.03797870 0.12120976
## 23      1 2.154435e-02 0.7511260 0.15629826 0.03803139 0.12366033
## 24      1 2.477076e-02 0.7477927 0.13832951 0.03797870 0.13261483
## 25      1 2.848036e-02 0.7413344 0.08101337 0.02783167 0.11211875
## 26      1 3.274549e-02 0.7446677 0.08648212 0.02407348 0.10592706
## 27      1 3.764936e-02 0.7413344 0.07062734 0.01675633 0.06974456
## 28      1 4.328761e-02 0.7381085 0.04556031 0.01950701 0.07830637
## 29      1 4.977024e-02 0.7380010 0.03287458 0.01429122 0.05779119
## 30      1 5.722368e-02 0.7348760 0.00625000 0.01478407 0.01976424
## 31      1 6.579332e-02 0.7348760 0.00000000 0.01478407 0.00000000
## 32      1 7.564633e-02 0.7348760 0.00000000 0.01478407 0.00000000
## 33      1 8.697490e-02 0.7348760 0.00000000 0.01478407 0.00000000
## 34      1 1.000000e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 35      1 1.149757e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 36      1 1.321941e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 37      1 1.519911e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 38      1 1.747528e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 39      1 2.009233e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 40      1 2.310130e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 41      1 2.656088e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 42      1 3.053856e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 43      1 3.511192e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 44      1 4.037017e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 45      1 4.641589e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 46      1 5.336699e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 47      1 6.135907e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 48      1 7.054802e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 49      1 8.111308e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 50      1 9.326033e-01 0.7348760 0.00000000 0.01478407 0.00000000
## 51      1 1.072267e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 52      1 1.232847e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 53      1 1.417474e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 54      1 1.629751e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 55      1 1.873817e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 56      1 2.154435e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 57      1 2.477076e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 58      1 2.848036e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 59      1 3.274549e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 60      1 3.764936e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 61      1 4.328761e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 62      1 4.977024e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 63      1 5.722368e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 64      1 6.579332e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 65      1 7.564633e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 66      1 8.697490e+00 0.7348760 0.00000000 0.01478407 0.00000000
## 67      1 1.000000e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 68      1 1.149757e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 69      1 1.321941e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 70      1 1.519911e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 71      1 1.747528e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 72      1 2.009233e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 73      1 2.310130e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 74      1 2.656088e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 75      1 3.053856e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 76      1 3.511192e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 77      1 4.037017e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 78      1 4.641589e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 79      1 5.336699e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 80      1 6.135907e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 81      1 7.054802e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 82      1 8.111308e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 83      1 9.326033e+01 0.7348760 0.00000000 0.01478407 0.00000000
## 84      1 1.072267e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 85      1 1.232847e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 86      1 1.417474e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 87      1 1.629751e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 88      1 1.873817e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 89      1 2.154435e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 90      1 2.477076e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 91      1 2.848036e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 92      1 3.274549e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 93      1 3.764936e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 94      1 4.328761e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 95      1 4.977024e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 96      1 5.722368e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 97      1 6.579332e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 98      1 7.564633e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 99      1 8.697490e+02 0.7348760 0.00000000 0.01478407 0.00000000
## 100     1 1.000000e+03 0.7348760 0.00000000 0.01478407 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  3.3  1.6  0.7
##          2 15.7 71.8  6.9
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.7508
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##               1       2     3
## q214       0.00 100.000  0.00
## q495      92.19   0.000  0.00
## q215      62.39   0.000  0.00
## q177      61.01   0.000  0.00
## q176       0.00  57.224 30.71
## q496      11.95  36.090  0.00
## q633       0.00  22.219  0.00
## q894       0.00  20.076  0.00
## q174       0.00  19.497  0.00
## race42    18.99   0.000  0.00
## q872      15.66   0.000 18.93
## q413      18.17   0.000  0.00
## q897       0.00  17.552  0.00
## age4      17.30   0.000  0.00
## age6      13.43   0.000  0.00
## q532       0.00  12.415  0.00
## q475      12.24   0.000  0.00
## q472       0.00   8.569 11.51
## qnothhpl2 10.67   0.000  0.00
## q302       0.00   8.561  0.00
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##                 x1
## q495      92.19041
## q215      62.39230
## q177      61.01496
## race42    18.98873
## q413      18.16752
## age4      17.29543
## q872      15.65613
## age6      13.42826
## q475      12.24094
## q496      11.95278
## qnothhpl2 10.67079
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##             x2
## q214 100.00000
## q176  57.22374
## q496  36.09019
## q633  22.21938
## q894  20.07588
## q174  19.49724
## q897  17.55170
## q532  12.41504
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
##            x3
## q176 30.71175
## q872 18.93355
## q472 11.51069

Pasco County, FL (PS)

df_ps = df_fit_district %>% 
  filter(sitename == "Pasco County, FL (PS)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_ps, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))

summary(model_lasso)
##             Length Class      Mode     
## a0          300    -none-     numeric  
## beta          3    -none-     list     
## dfmat       300    -none-     numeric  
## df          100    -none-     numeric  
## dim           2    -none-     numeric  
## lambda      100    -none-     numeric  
## dev.ratio   100    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha     lambda
## 27     1 0.03764936
model_lasso$results
##     alpha       lambda  Accuracy      Kappa AccuracySD   KappaSD
## 1       1 1.000000e-03 0.6744361 0.06305505 0.12480445 0.2826758
## 2       1 1.149757e-03 0.6694361 0.06016209 0.12687657 0.2852072
## 3       1 1.321941e-03 0.6694361 0.06016209 0.12687657 0.2852072
## 4       1 1.519911e-03 0.6644361 0.06715416 0.13084027 0.2798785
## 5       1 1.747528e-03 0.6696992 0.07012059 0.12562142 0.2800171
## 6       1 2.009233e-03 0.6744612 0.06743390 0.12438450 0.2739468
## 7       1 2.310130e-03 0.6744612 0.06743390 0.12438450 0.2739468
## 8       1 2.656088e-03 0.6794612 0.07223582 0.11974390 0.2686305
## 9       1 3.053856e-03 0.6847243 0.07799864 0.11593106 0.2697944
## 10      1 3.511192e-03 0.6897243 0.08426672 0.11772740 0.2715404
## 11      1 4.037017e-03 0.6894862 0.06795587 0.11119609 0.2373229
## 12      1 4.641589e-03 0.7047494 0.08351140 0.09293297 0.2282899
## 13      1 5.336699e-03 0.7150125 0.09621737 0.09678652 0.2449530
## 14      1 6.135907e-03 0.7202757 0.10540312 0.09949852 0.2539514
## 15      1 7.054802e-03 0.7202757 0.09538523 0.09949852 0.2594680
## 16      1 8.111308e-03 0.7400125 0.11516800 0.07677702 0.2450185
## 17      1 9.326033e-03 0.7500125 0.14790817 0.07749544 0.2533368
## 18      1 1.072267e-02 0.7550125 0.13908277 0.05405619 0.1974476
## 19      1 1.232847e-02 0.7650125 0.14244914 0.05195739 0.1889369
## 20      1 1.417474e-02 0.7700125 0.15055725 0.04719332 0.1797970
## 21      1 1.629751e-02 0.7750376 0.14280297 0.03420906 0.1316646
## 22      1 1.873817e-02 0.7650376 0.10787224 0.04659501 0.1308148
## 23      1 2.154435e-02 0.7600376 0.07994431 0.04508463 0.1194752
## 24      1 2.477076e-02 0.7600376 0.07994431 0.04508463 0.1194752
## 25      1 2.848036e-02 0.7700376 0.09591241 0.04121232 0.1388129
## 26      1 3.274549e-02 0.7803008 0.11620454 0.04020089 0.1570041
## 27      1 3.764936e-02 0.7853008 0.12609969 0.03910801 0.1644368
## 28      1 4.328761e-02 0.7803008 0.09817176 0.04020089 0.1591619
## 29      1 4.977024e-02 0.7803008 0.09817176 0.04020089 0.1591619
## 30      1 5.722368e-02 0.7803008 0.09817176 0.04020089 0.1591619
## 31      1 6.579332e-02 0.7750376 0.06368900 0.03420906 0.1353598
## 32      1 7.564633e-02 0.7652757 0.00000000 0.01990623 0.0000000
## 33      1 8.697490e-02 0.7652757 0.00000000 0.01990623 0.0000000
## 34      1 1.000000e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 35      1 1.149757e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 36      1 1.321941e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 37      1 1.519911e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 38      1 1.747528e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 39      1 2.009233e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 40      1 2.310130e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 41      1 2.656088e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 42      1 3.053856e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 43      1 3.511192e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 44      1 4.037017e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 45      1 4.641589e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 46      1 5.336699e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 47      1 6.135907e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 48      1 7.054802e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 49      1 8.111308e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 50      1 9.326033e-01 0.7652757 0.00000000 0.01990623 0.0000000
## 51      1 1.072267e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 52      1 1.232847e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 53      1 1.417474e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 54      1 1.629751e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 55      1 1.873817e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 56      1 2.154435e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 57      1 2.477076e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 58      1 2.848036e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 59      1 3.274549e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 60      1 3.764936e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 61      1 4.328761e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 62      1 4.977024e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 63      1 5.722368e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 64      1 6.579332e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 65      1 7.564633e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 66      1 8.697490e+00 0.7652757 0.00000000 0.01990623 0.0000000
## 67      1 1.000000e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 68      1 1.149757e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 69      1 1.321941e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 70      1 1.519911e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 71      1 1.747528e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 72      1 2.009233e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 73      1 2.310130e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 74      1 2.656088e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 75      1 3.053856e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 76      1 3.511192e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 77      1 4.037017e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 78      1 4.641589e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 79      1 5.336699e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 80      1 6.135907e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 81      1 7.054802e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 82      1 8.111308e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 83      1 9.326033e+01 0.7652757 0.00000000 0.01990623 0.0000000
## 84      1 1.072267e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 85      1 1.232847e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 86      1 1.417474e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 87      1 1.629751e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 88      1 1.873817e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 89      1 2.154435e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 90      1 2.477076e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 91      1 2.848036e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 92      1 3.274549e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 93      1 3.764936e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 94      1 4.328761e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 95      1 4.977024e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 96      1 5.722368e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 97      1 6.579332e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 98      1 7.564633e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 99      1 8.697490e+02 0.7652757 0.00000000 0.01990623 0.0000000
## 100     1 1.000000e+03 0.7652757 0.00000000 0.01990623 0.0000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  2.0  0.0  0.0
##          2 17.0 76.5  4.5
##          3  0.0  0.0  0.0
##                            
##  Accuracy (average) : 0.785
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##                  1       2     3
## q178      100.0000  0.0000  0.00
## q475        0.0000  0.0000 74.58
## q493       17.7502  0.0000  0.00
## sex2       17.3787  0.0000  0.00
## qnothhpl2   0.0000 14.8690  0.00
## q502        0.0000 14.0383  0.00
## grade4     12.5549  0.0000  0.00
## q302       10.6132  0.0000  0.00
## q492        7.2529  0.0000  0.00
## q653        0.0000  3.5920  0.00
## q873        0.0000  3.4580  0.00
## q232        2.0557  0.0000  0.00
## q214        0.5358  0.0000  0.00
## q173        0.0000  0.4988  0.00
## q654        0.2829  0.0000  0.00
## q897        0.0000  0.1906  0.00
## q494        0.0000  0.0000  0.00
## bmi         0.0000  0.0000  0.00
## q172        0.0000  0.0000  0.00
## age7        0.0000  0.0000  0.00
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##               x1
## q178   100.00000
## q493    17.75018
## sex2    17.37868
## grade4  12.55491
## q302    10.61321
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##                 x2
## qnothhpl2 14.86900
## q502      14.03834
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
##            x3
## q475 74.57636

Philadelphia, PA (PH)

df_ph = df_fit_district %>% 
  filter(sitename == "Philadelphia, PA (PH)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_ph, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))

summary(model_lasso)
##             Length Class      Mode     
## a0          300    -none-     numeric  
## beta          3    -none-     list     
## dfmat       300    -none-     numeric  
## df          100    -none-     numeric  
## dim           2    -none-     numeric  
## lambda      100    -none-     numeric  
## dev.ratio   100    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##     alpha lambda
## 100     1   1000
model_lasso$results
##     alpha       lambda  Accuracy       Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.5186683  0.07733753 0.09309386 0.17854681
## 2       1 1.149757e-03 0.5186683  0.08056257 0.09309386 0.17650232
## 3       1 1.321941e-03 0.5186683  0.08056257 0.09309386 0.17650232
## 4       1 1.519911e-03 0.5186683  0.07505331 0.09309386 0.17411897
## 5       1 1.747528e-03 0.5186683  0.07505331 0.09309386 0.17411897
## 6       1 2.009233e-03 0.5186683  0.07505331 0.09309386 0.17411897
## 7       1 2.310130e-03 0.4947712  0.03718210 0.09267842 0.17139319
## 8       1 2.656088e-03 0.4947712  0.03316585 0.09267842 0.17662240
## 9       1 3.053856e-03 0.4947712  0.03316585 0.09267842 0.17662240
## 10      1 3.511192e-03 0.5065359  0.05741450 0.10430248 0.19218622
## 11      1 4.037017e-03 0.5183007  0.07410698 0.10996461 0.20560436
## 12      1 4.641589e-03 0.5352941  0.09477713 0.09454936 0.18662554
## 13      1 5.336699e-03 0.5408497  0.08651779 0.07165156 0.14526889
## 14      1 6.135907e-03 0.5408497  0.08651779 0.07165156 0.14526889
## 15      1 7.054802e-03 0.5467320  0.10034682 0.07809857 0.15973214
## 16      1 8.111308e-03 0.5460376  0.09870028 0.08385103 0.15615189
## 17      1 9.326033e-03 0.5460376  0.09564373 0.08385103 0.15692277
## 18      1 1.072267e-02 0.5571078  0.11336756 0.10926043 0.19690213
## 19      1 1.232847e-02 0.5515523  0.09662444 0.11453230 0.21440645
## 20      1 1.417474e-02 0.5688725  0.12072345 0.10914199 0.20583180
## 21      1 1.629751e-02 0.5865196  0.14880652 0.10569709 0.19714452
## 22      1 1.873817e-02 0.5865196  0.14950821 0.11936337 0.22498409
## 23      1 2.154435e-02 0.5812908  0.13199388 0.12022318 0.22947303
## 24      1 2.477076e-02 0.5875408  0.12946782 0.09435755 0.18888244
## 25      1 2.848036e-02 0.5882353  0.11582140 0.10802454 0.24372226
## 26      1 3.274549e-02 0.5937908  0.11368286 0.08932528 0.19955050
## 27      1 3.764936e-02 0.5931373  0.08691107 0.07399794 0.15166371
## 28      1 4.328761e-02 0.5800245  0.02504156 0.07833592 0.15307247
## 29      1 4.977024e-02 0.5623366 -0.03953847 0.07546674 0.11730498
## 30      1 5.722368e-02 0.5748366 -0.03337391 0.03584689 0.05822237
## 31      1 6.579332e-02 0.5928922 -0.01056911 0.02617228 0.03342245
## 32      1 7.564633e-02 0.5987745  0.00000000 0.01418451 0.00000000
## 33      1 8.697490e-02 0.5987745  0.00000000 0.01418451 0.00000000
## 34      1 1.000000e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 35      1 1.149757e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 36      1 1.321941e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 37      1 1.519911e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 38      1 1.747528e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 39      1 2.009233e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 40      1 2.310130e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 41      1 2.656088e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 42      1 3.053856e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 43      1 3.511192e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 44      1 4.037017e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 45      1 4.641589e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 46      1 5.336699e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 47      1 6.135907e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 48      1 7.054802e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 49      1 8.111308e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 50      1 9.326033e-01 0.5987745  0.00000000 0.01418451 0.00000000
## 51      1 1.072267e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 52      1 1.232847e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 53      1 1.417474e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 54      1 1.629751e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 55      1 1.873817e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 56      1 2.154435e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 57      1 2.477076e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 58      1 2.848036e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 59      1 3.274549e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 60      1 3.764936e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 61      1 4.328761e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 62      1 4.977024e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 63      1 5.722368e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 64      1 6.579332e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 65      1 7.564633e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 66      1 8.697490e+00 0.5987745  0.00000000 0.01418451 0.00000000
## 67      1 1.000000e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 68      1 1.149757e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 69      1 1.321941e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 70      1 1.519911e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 71      1 1.747528e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 72      1 2.009233e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 73      1 2.310130e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 74      1 2.656088e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 75      1 3.053856e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 76      1 3.511192e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 77      1 4.037017e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 78      1 4.641589e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 79      1 5.336699e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 80      1 6.135907e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 81      1 7.054802e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 82      1 8.111308e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 83      1 9.326033e+01 0.5987745  0.00000000 0.01418451 0.00000000
## 84      1 1.072267e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 85      1 1.232847e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 86      1 1.417474e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 87      1 1.629751e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 88      1 1.873817e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 89      1 2.154435e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 90      1 2.477076e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 91      1 2.848036e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 92      1 3.274549e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 93      1 3.764936e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 94      1 4.328761e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 95      1 4.977024e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 96      1 5.722368e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 97      1 6.579332e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 98      1 7.564633e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 99      1 8.697490e+02 0.5987745  0.00000000 0.01418451 0.00000000
## 100     1 1.000000e+03 0.5987745  0.00000000 0.01418451 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1 59.9 34.3  5.8
##          2  0.0  0.0  0.0
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.5988
varImp(model_lasso, scale = FALSE)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##        1 2 3
## q414   0 0 0
## race43 0 0 0
## race44 0 0 0
## age7   0 0 0
## q475   0 0 0
## sex2   0 0 0
## q492   0 0 0
## q895   0 0 0
## q493   0 0 0
## q174   0 0 0
## q262   0 0 0
## q413   0 0 0
## age5   0 0 0
## q173   0 0 0
## q872   0 0 0
## age4   0 0 0
## grade4 0 0 0
## q496   0 0 0
## q896   0 0 0
## q495   0 0 0
plot(varImp(model_lasso))
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning -Inf

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
## [1] x1
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
## [1] x2
## <0 rows> (or 0-length row.names)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
## [1] x3
## <0 rows> (or 0-length row.names)

Portland, OR (PO)

df_po = df_fit_district %>% 
  filter(sitename == "Portland, OR (PO)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_po, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))

summary(model_lasso)
##             Length Class      Mode     
## a0          300    -none-     numeric  
## beta          3    -none-     list     
## dfmat       300    -none-     numeric  
## df          100    -none-     numeric  
## dim           2    -none-     numeric  
## lambda      100    -none-     numeric  
## dev.ratio   100    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha     lambda
## 32     1 0.07564633
model_lasso$results
##     alpha       lambda  Accuracy       Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.5119481 0.049588805 0.07799131 0.13037224
## 2       1 1.149757e-03 0.5119481 0.049588805 0.07799131 0.13037224
## 3       1 1.321941e-03 0.5119481 0.049588805 0.07799131 0.13037224
## 4       1 1.519911e-03 0.5119481 0.049588805 0.07799131 0.13037224
## 5       1 1.747528e-03 0.5167100 0.057319068 0.08021795 0.13453187
## 6       1 2.009233e-03 0.5260173 0.069394380 0.07252298 0.12912243
## 7       1 2.310130e-03 0.5307792 0.071354720 0.07042067 0.12754127
## 8       1 2.656088e-03 0.5353247 0.075704530 0.06967626 0.13010073
## 9       1 3.053856e-03 0.5491775 0.096759630 0.07677614 0.14824267
## 10      1 3.511192e-03 0.5489394 0.098789365 0.07807054 0.16471786
## 11      1 4.037017e-03 0.5580303 0.113719505 0.08277110 0.17139176
## 12      1 4.641589e-03 0.5720996 0.132875108 0.09696618 0.19134619
## 13      1 5.336699e-03 0.5673377 0.125517750 0.09556355 0.18892822
## 14      1 6.135907e-03 0.5584632 0.096351228 0.09163119 0.16738034
## 15      1 7.054802e-03 0.5679870 0.117661308 0.09786574 0.18411159
## 16      1 8.111308e-03 0.5632251 0.106210383 0.09625038 0.17749912
## 17      1 9.326033e-03 0.5632251 0.106210383 0.09625038 0.17749912
## 18      1 1.072267e-02 0.5677706 0.107155586 0.09874403 0.19076721
## 19      1 1.232847e-02 0.5907143 0.144470629 0.09709783 0.18945419
## 20      1 1.417474e-02 0.5952597 0.153765524 0.09337181 0.18211554
## 21      1 1.629751e-02 0.5909307 0.144398050 0.09100126 0.17316109
## 22      1 1.873817e-02 0.5909307 0.144398050 0.09100126 0.17316109
## 23      1 2.154435e-02 0.5907143 0.138862828 0.08086323 0.15511653
## 24      1 2.477076e-02 0.6000216 0.150803791 0.08474169 0.16352387
## 25      1 2.848036e-02 0.5959091 0.136925087 0.08153868 0.15743209
## 26      1 3.274549e-02 0.5961255 0.133543740 0.08854236 0.16920907
## 27      1 3.764936e-02 0.5866234 0.094281657 0.06543195 0.12224392
## 28      1 4.328761e-02 0.5723377 0.047652162 0.07275173 0.16003689
## 29      1 4.977024e-02 0.6002597 0.081768537 0.07652676 0.17614662
## 30      1 5.722368e-02 0.5911688 0.042013821 0.05104074 0.12971087
## 31      1 6.579332e-02 0.6050216 0.049335423 0.03829760 0.09470555
## 32      1 7.564633e-02 0.6102165 0.036275665 0.02117056 0.06672942
## 33      1 8.697490e-02 0.6011255 0.005617978 0.01694922 0.01776560
## 34      1 1.000000e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 35      1 1.149757e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 36      1 1.321941e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 37      1 1.519911e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 38      1 1.747528e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 39      1 2.009233e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 40      1 2.310130e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 41      1 2.656088e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 42      1 3.053856e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 43      1 3.511192e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 44      1 4.037017e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 45      1 4.641589e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 46      1 5.336699e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 47      1 6.135907e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 48      1 7.054802e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 49      1 8.111308e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 50      1 9.326033e-01 0.6011255 0.000000000 0.01694922 0.00000000
## 51      1 1.072267e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 52      1 1.232847e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 53      1 1.417474e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 54      1 1.629751e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 55      1 1.873817e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 56      1 2.154435e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 57      1 2.477076e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 58      1 2.848036e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 59      1 3.274549e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 60      1 3.764936e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 61      1 4.328761e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 62      1 4.977024e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 63      1 5.722368e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 64      1 6.579332e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 65      1 7.564633e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 66      1 8.697490e+00 0.6011255 0.000000000 0.01694922 0.00000000
## 67      1 1.000000e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 68      1 1.149757e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 69      1 1.321941e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 70      1 1.519911e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 71      1 1.747528e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 72      1 2.009233e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 73      1 2.310130e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 74      1 2.656088e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 75      1 3.053856e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 76      1 3.511192e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 77      1 4.037017e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 78      1 4.641589e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 79      1 5.336699e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 80      1 6.135907e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 81      1 7.054802e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 82      1 8.111308e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 83      1 9.326033e+01 0.6011255 0.000000000 0.01694922 0.00000000
## 84      1 1.072267e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 85      1 1.232847e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 86      1 1.417474e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 87      1 1.629751e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 88      1 1.873817e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 89      1 2.154435e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 90      1 2.477076e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 91      1 2.848036e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 92      1 3.274549e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 93      1 3.764936e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 94      1 4.328761e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 95      1 4.977024e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 96      1 5.722368e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 97      1 6.579332e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 98      1 7.564633e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 99      1 8.697490e+02 0.6011255 0.000000000 0.01694922 0.00000000
## 100     1 1.000000e+03 0.6011255 0.000000000 0.01694922 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  1.9  0.9  0.0
##          2 33.8 59.2  4.2
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.6103
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##               1      2 3
## age7       0.00 100.00 0
## grade2     0.00  95.15 0
## qnothhpl2 90.60   0.00 0
## sex2      75.05   0.00 0
## q472       0.00   0.00 0
## q172       0.00   0.00 0
## q173       0.00   0.00 0
## grade3     0.00   0.00 0
## q494       0.00   0.00 0
## grade4     0.00   0.00 0
## q496       0.00   0.00 0
## q502       0.00   0.00 0
## q178       0.00   0.00 0
## q414       0.00   0.00 0
## q417       0.00   0.00 0
## age5       0.00   0.00 0
## q177       0.00   0.00 0
## q894       0.00   0.00 0
## age4       0.00   0.00 0
## race44     0.00   0.00 0
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##                 x1
## qnothhpl2 90.60028
## sex2      75.04838
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##               x2
## age7   100.00000
## grade2  95.15292
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
## [1] x3
## <0 rows> (or 0-length row.names)

San Francisco, CA (SF)

df_sf = df_fit_district %>% 
  filter(sitename == "San Francisco, CA (SF)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_sf, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))

summary(model_lasso)
##             Length Class      Mode     
## a0          297    -none-     numeric  
## beta          3    -none-     list     
## dfmat       297    -none-     numeric  
## df           99    -none-     numeric  
## dim           2    -none-     numeric  
## lambda       99    -none-     numeric  
## dev.ratio    99    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha     lambda
## 27     1 0.03764936
model_lasso$results
##     alpha       lambda  Accuracy         Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.4385338 -0.0305150648 0.08639463 0.15493463
## 2       1 1.149757e-03 0.4540602 -0.0056809037 0.08870194 0.14188284
## 3       1 1.321941e-03 0.4588221 -0.0006961685 0.09126322 0.14793956
## 4       1 1.519911e-03 0.4635840  0.0008243599 0.08501653 0.15015252
## 5       1 1.747528e-03 0.4781078  0.0113951840 0.08594813 0.15930363
## 6       1 2.009233e-03 0.4781078  0.0149564375 0.08594813 0.16037042
## 7       1 2.310130e-03 0.4781078  0.0149564375 0.08594813 0.16037042
## 8       1 2.656088e-03 0.4783459  0.0080587209 0.08134444 0.14629250
## 9       1 3.053856e-03 0.4835840  0.0154844107 0.09982596 0.18788852
## 10      1 3.511192e-03 0.4835840  0.0154844107 0.09982596 0.18788852
## 11      1 4.037017e-03 0.4838221  0.0108332740 0.09265504 0.18436936
## 12      1 4.641589e-03 0.4790602  0.0048785349 0.09693491 0.18344954
## 13      1 5.336699e-03 0.4890602  0.0101974854 0.08170381 0.14836123
## 14      1 6.135907e-03 0.4995614  0.0245329334 0.09452813 0.16113154
## 15      1 7.054802e-03 0.4940602  0.0128641126 0.08629989 0.13526090
## 16      1 8.111308e-03 0.4992982  0.0210334763 0.10827821 0.17293564
## 17      1 9.326033e-03 0.5140602  0.0457386506 0.10295899 0.17227567
## 18      1 1.072267e-02 0.5145363  0.0403617586 0.13188800 0.22892790
## 19      1 1.232847e-02 0.5097744  0.0258727868 0.14157188 0.24015763
## 20      1 1.417474e-02 0.5092982  0.0226464899 0.11287056 0.17333188
## 21      1 1.629751e-02 0.5390852  0.0674873815 0.11748087 0.18143199
## 22      1 1.873817e-02 0.5446115  0.0718968313 0.11407382 0.18569243
## 23      1 2.154435e-02 0.5638972  0.1013504551 0.10733584 0.16967040
## 24      1 2.477076e-02 0.5588972  0.0889134109 0.11102252 0.18418936
## 25      1 2.848036e-02 0.5834461  0.1268433250 0.11088081 0.18815243
## 26      1 3.274549e-02 0.5829449  0.1170980351 0.10237424 0.17088851
## 27      1 3.764936e-02 0.5979449  0.1363629518 0.09832778 0.16983032
## 28      1 4.328761e-02 0.5791353  0.0967020848 0.12374845 0.21514623
## 29      1 4.977024e-02 0.5736341  0.0719850006 0.10167298 0.16162266
## 30      1 5.722368e-02 0.5783960  0.0651560273 0.09233196 0.15591646
## 31      1 6.579332e-02 0.5771805  0.0357077911 0.04212045 0.07640476
## 32      1 7.564633e-02 0.5719424  0.0138045661 0.03039285 0.05965034
## 33      1 8.697490e-02 0.5717043  0.0000000000 0.02514556 0.00000000
## 34      1 1.000000e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 35      1 1.149757e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 36      1 1.321941e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 37      1 1.519911e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 38      1 1.747528e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 39      1 2.009233e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 40      1 2.310130e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 41      1 2.656088e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 42      1 3.053856e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 43      1 3.511192e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 44      1 4.037017e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 45      1 4.641589e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 46      1 5.336699e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 47      1 6.135907e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 48      1 7.054802e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 49      1 8.111308e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 50      1 9.326033e-01 0.5717043  0.0000000000 0.02514556 0.00000000
## 51      1 1.072267e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 52      1 1.232847e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 53      1 1.417474e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 54      1 1.629751e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 55      1 1.873817e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 56      1 2.154435e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 57      1 2.477076e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 58      1 2.848036e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 59      1 3.274549e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 60      1 3.764936e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 61      1 4.328761e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 62      1 4.977024e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 63      1 5.722368e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 64      1 6.579332e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 65      1 7.564633e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 66      1 8.697490e+00 0.5717043  0.0000000000 0.02514556 0.00000000
## 67      1 1.000000e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 68      1 1.149757e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 69      1 1.321941e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 70      1 1.519911e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 71      1 1.747528e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 72      1 2.009233e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 73      1 2.310130e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 74      1 2.656088e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 75      1 3.053856e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 76      1 3.511192e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 77      1 4.037017e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 78      1 4.641589e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 79      1 5.336699e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 80      1 6.135907e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 81      1 7.054802e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 82      1 8.111308e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 83      1 9.326033e+01 0.5717043  0.0000000000 0.02514556 0.00000000
## 84      1 1.072267e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 85      1 1.232847e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 86      1 1.417474e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 87      1 1.629751e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 88      1 1.873817e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 89      1 2.154435e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 90      1 2.477076e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 91      1 2.848036e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 92      1 3.274549e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 93      1 3.764936e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 94      1 4.328761e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 95      1 4.977024e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 96      1 5.722368e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 97      1 6.579332e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 98      1 7.564633e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 99      1 8.697490e+02 0.5717043  0.0000000000 0.02514556 0.00000000
## 100     1 1.000000e+03 0.5717043  0.0000000000 0.02514556 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  8.4  5.9  2.0
##          2 27.6 51.2  4.9
##          3  0.0  0.0  0.0
##                             
##  Accuracy (average) : 0.5961
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##                 1      2   3
## q492       0.6318  0.000 100
## q178      92.3574  0.000   0
## q415      85.0255  0.000   0
## qnothhpl2  0.0000 70.656   0
## q302       0.0000 44.693   0
## q474      42.3052  0.000   0
## age4      15.7485 42.257   0
## q175      40.1721  0.000   0
## q495       0.0000 33.948   0
## q653       0.0000 21.010   0
## q212       0.0000 19.264   0
## age6      15.9270  0.000   0
## q873      12.7954  0.000   0
## race43     0.0000  9.292   0
## q472       0.0000  7.647   0
## q476       7.5538  0.000   0
## q654       0.0000  6.147   0
## q494       1.6113  0.000   0
## q522       0.0000  0.000   0
## q172       0.0000  0.000   0
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##            x1
## q178 92.35737
## q415 85.02549
## q474 42.30519
## q175 40.17212
## age6 15.92697
## age4 15.74855
## q873 12.79543
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##                 x2
## qnothhpl2 70.65601
## q302      44.69343
## age4      42.25721
## q495      33.94785
## q653      21.01046
## q212      19.26352
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
##       x3
## q492 100

Shelby County, TN (ST)

df_sf = df_fit_district %>% 
  filter(sitename == "Shelby County, TN (ST)") %>% 
  select(-sitename, -year)


set.seed(123)
#Create grid to search lambda
lambda <- 10^seq(-3,3, length = 100)

# Specify training control
train_control_lasso <- trainControl(method = "cv", number = 10)

model_lasso = train(q85 ~.,
                    df_sf, 
                    method = "glmnet", 
                    trControl = train_control_lasso, 
                    tuneGrid = expand.grid(alpha = 1, lambda = lambda))
## Warning: from glmnet C++ code (error code -92); Convergence for 92th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -75); Convergence for 75th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned

## Warning: from glmnet C++ code (error code -75); Convergence for 75th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -80); Convergence for 80th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -77); Convergence for 77th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: from glmnet C++ code (error code -74); Convergence for 74th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
summary(model_lasso)
##             Length Class      Mode     
## a0          300    -none-     numeric  
## beta          3    -none-     list     
## dfmat       300    -none-     numeric  
## df          100    -none-     numeric  
## dim           2    -none-     numeric  
## lambda      100    -none-     numeric  
## dev.ratio   100    -none-     numeric  
## nulldev       1    -none-     numeric  
## npasses       1    -none-     numeric  
## jerr          1    -none-     numeric  
## offset        1    -none-     logical  
## classnames    3    -none-     character
## grouped       1    -none-     logical  
## call          5    -none-     call     
## nobs          1    -none-     numeric  
## lambdaOpt     1    -none-     numeric  
## xNames       58    -none-     character
## problemType   1    -none-     character
## tuneValue     2    data.frame list     
## obsLevels     3    -none-     character
## param         0    -none-     list
model_lasso$bestTune
##    alpha     lambda
## 29     1 0.04977024
model_lasso$results
##     alpha       lambda  Accuracy        Kappa AccuracySD    KappaSD
## 1       1 1.000000e-03 0.5359804  0.064087964 0.10053693 0.22262612
## 2       1 1.149757e-03 0.5426471  0.077180382 0.08757591 0.20483333
## 3       1 1.321941e-03 0.5488971  0.085114718 0.09130115 0.21056330
## 4       1 1.519911e-03 0.5488971  0.076460694 0.09130115 0.20989016
## 5       1 1.747528e-03 0.5496814  0.085902774 0.09611268 0.21649502
## 6       1 2.009233e-03 0.5437990  0.068546978 0.10304157 0.20660795
## 7       1 2.310130e-03 0.5496814  0.076175183 0.11096570 0.21391089
## 8       1 2.656088e-03 0.5496814  0.076175183 0.11096570 0.21391089
## 9       1 3.053856e-03 0.5430637  0.059014393 0.11057306 0.22177050
## 10      1 3.511192e-03 0.5493137  0.051422068 0.10140418 0.23342934
## 11      1 4.037017e-03 0.5555637  0.052892305 0.10828034 0.24127840
## 12      1 4.641589e-03 0.5618137  0.075068327 0.10650605 0.22170740
## 13      1 5.336699e-03 0.5743627  0.090828929 0.10686482 0.23354335
## 14      1 6.135907e-03 0.5935294  0.111820618 0.08856979 0.20906639
## 15      1 7.054802e-03 0.5927451  0.102337102 0.09543254 0.21084300
## 16      1 8.111308e-03 0.5994118  0.098225983 0.07789399 0.17812669
## 17      1 9.326033e-03 0.5994118  0.098225983 0.07789399 0.17812669
## 18      1 1.072267e-02 0.6174265  0.120351794 0.06684309 0.17940402
## 19      1 1.232847e-02 0.6424265  0.157418338 0.06476346 0.16423768
## 20      1 1.417474e-02 0.6490931  0.165007624 0.06332622 0.16524046
## 21      1 1.629751e-02 0.6490931  0.165007624 0.06332622 0.16524046
## 22      1 1.873817e-02 0.6553431  0.166659698 0.07617296 0.19072203
## 23      1 2.154435e-02 0.6427941  0.138685686 0.07678515 0.17720258
## 24      1 2.477076e-02 0.6432108  0.125111341 0.07948066 0.18763904
## 25      1 2.848036e-02 0.6494608  0.121087230 0.07474502 0.19267967
## 26      1 3.274549e-02 0.6557108  0.118624848 0.07508468 0.17008730
## 27      1 3.764936e-02 0.6560784  0.098331337 0.07837483 0.16724577
## 28      1 4.328761e-02 0.6623284  0.077952516 0.05127740 0.12611030
## 29      1 4.977024e-02 0.6694118  0.036128529 0.03390568 0.11385368
## 30      1 5.722368e-02 0.6627451  0.003780585 0.02543725 0.08043915
## 31      1 6.579332e-02 0.6564951 -0.017011494 0.02634123 0.03689637
## 32      1 7.564633e-02 0.6627451 -0.010344828 0.02543725 0.03271322
## 33      1 8.697490e-02 0.6689951  0.000000000 0.02265924 0.00000000
## 34      1 1.000000e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 35      1 1.149757e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 36      1 1.321941e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 37      1 1.519911e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 38      1 1.747528e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 39      1 2.009233e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 40      1 2.310130e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 41      1 2.656088e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 42      1 3.053856e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 43      1 3.511192e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 44      1 4.037017e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 45      1 4.641589e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 46      1 5.336699e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 47      1 6.135907e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 48      1 7.054802e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 49      1 8.111308e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 50      1 9.326033e-01 0.6689951  0.000000000 0.02265924 0.00000000
## 51      1 1.072267e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 52      1 1.232847e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 53      1 1.417474e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 54      1 1.629751e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 55      1 1.873817e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 56      1 2.154435e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 57      1 2.477076e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 58      1 2.848036e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 59      1 3.274549e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 60      1 3.764936e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 61      1 4.328761e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 62      1 4.977024e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 63      1 5.722368e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 64      1 6.579332e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 65      1 7.564633e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 66      1 8.697490e+00 0.6689951  0.000000000 0.02265924 0.00000000
## 67      1 1.000000e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 68      1 1.149757e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 69      1 1.321941e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 70      1 1.519911e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 71      1 1.747528e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 72      1 2.009233e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 73      1 2.310130e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 74      1 2.656088e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 75      1 3.053856e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 76      1 3.511192e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 77      1 4.037017e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 78      1 4.641589e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 79      1 5.336699e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 80      1 6.135907e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 81      1 7.054802e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 82      1 8.111308e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 83      1 9.326033e+01 0.6689951  0.000000000 0.02265924 0.00000000
## 84      1 1.072267e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 85      1 1.232847e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 86      1 1.417474e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 87      1 1.629751e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 88      1 1.873817e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 89      1 2.154435e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 90      1 2.477076e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 91      1 2.848036e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 92      1 3.274549e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 93      1 3.764936e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 94      1 4.328761e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 95      1 4.977024e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 96      1 5.722368e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 97      1 6.579332e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 98      1 7.564633e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 99      1 8.697490e+02 0.6689951  0.000000000 0.02265924 0.00000000
## 100     1 1.000000e+03 0.6689951  0.000000000 0.02265924 0.00000000
#Visualize accuracy versus values of C
plot(model_lasso)

#Obtain metrics of accuracy from training
confusionMatrix(model_lasso)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction    1    2    3
##          1  1.2  0.6  0.6
##          2 25.0 65.6  6.2
##          3  0.0  0.6  0.0
##                             
##  Accuracy (average) : 0.6687
varImp(model_lasso)
## glmnet variable importance
## 
##   variables are sorted by maximum importance across the classes
##   only 20 most important variables shown (out of 58)
## 
##                1      2      3
## q216       0.000  0.000 100.00
## q176      86.440  0.000   0.00
## q414      78.603  0.000   0.00
## q472       0.000 41.690   0.00
## q178       0.000  4.219  41.63
## q897       0.000 25.178   0.00
## race42     0.000 23.406   0.00
## sex2      18.158  0.000   0.00
## q473      15.500 13.745   0.00
## grade4     0.000  6.549   0.00
## q172       4.106  0.000   0.00
## q412       0.000  3.371   0.00
## q476       0.000  2.860   0.00
## qnothhpl2  2.461  0.000   0.00
## q475       2.031  0.000   0.00
## q502       0.000  0.000   0.00
## q173       0.000  0.000   0.00
## q174       0.000  0.000   0.00
## age7       0.000  0.000   0.00
## q873       0.000  0.000   0.00
plot(varImp(model_lasso))

var_importance = varImp(model_lasso)
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x1 > 10) %>% 
  select(x1) %>% 
  arrange(desc(x1))
##            x1
## q176 86.44038
## q414 78.60256
## sex2 18.15770
## q473 15.50019
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x2 > 10) %>% 
  select(x2) %>% 
  arrange(desc(x2))
##              x2
## q472   41.68957
## q897   25.17849
## race42 23.40621
## q473   13.74520
var_importance$importance %>% 
  janitor::clean_names() %>% 
  filter(x3 > 10) %>% 
  select(x3) %>% 
  arrange(desc(x3))
##             x3
## q216 100.00000
## q178  41.63071